Maybe I can still help someone who is facing this issue, because this is a very old problem.
I also needed this and wrote a behavior to take care of this. So here is the behavior:
public class StretchMaxWidthBehavior : Behavior<FrameworkElement> { protected override void OnAttached() { base.OnAttached(); ((FrameworkElement)this.AssociatedObject.Parent).SizeChanged += this.OnSizeChanged; } protected override void OnDetaching() { base.OnDetaching(); ((FrameworkElement)this.AssociatedObject.Parent).SizeChanged -= this.OnSizeChanged; } private void OnSizeChanged(object sender, SizeChangedEventArgs e) { this.SetAlignments(); } private void SetAlignments() { var slot = LayoutInformation.GetLayoutSlot(this.AssociatedObject); var newWidth = slot.Width; var newHeight = slot.Height; if (!double.IsInfinity(this.AssociatedObject.MaxWidth)) { if (this.AssociatedObject.MaxWidth < newWidth) { this.AssociatedObject.HorizontalAlignment = HorizontalAlignment.Left; this.AssociatedObject.Width = this.AssociatedObject.MaxWidth; } else { this.AssociatedObject.HorizontalAlignment = HorizontalAlignment.Stretch; this.AssociatedObject.Width = double.NaN; } } if (!double.IsInfinity(this.AssociatedObject.MaxHeight)) { if (this.AssociatedObject.MaxHeight < newHeight) { this.AssociatedObject.VerticalAlignment = VerticalAlignment.Top; this.AssociatedObject.Height = this.AssociatedObject.MaxHeight; } else { this.AssociatedObject.VerticalAlignment = VerticalAlignment.Stretch; this.AssociatedObject.Height = double.NaN; } } } }
Then you can use it like this:
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="Label" /> <TextBox Grid.Column="1" MaxWidth="600"> <i:Interaction.Behaviors> <cbh:StretchMaxWidthBehavior/> </i:Interaction.Behaviors> </TextBox> </Grid>
Finally, forget to use the System.Windows.Interactivity names of the System.Windows.Interactivity names to use the behavior.
YC Jun 19 '18 at 12:44 2018-06-19 12:44
source share