HorizontalAlignment=Stretch, MaxWidth, and Left aligned at the same time?

前端 未结 8 2257
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 14:47

This seems like it should be easy but I\'m stumped. In WPF, I\'d like a TextBox that stretches to the width of it\'s parent, but only to a maximum width. The problem is that

相关标签:
8条回答
  • 2020-12-04 15:26

    I would use SharedSizeGroup

    <Grid>
        <Grid.ColumnDefinition>
            <ColumnDefinition SharedSizeGroup="col1"></ColumnDefinition>  
            <ColumnDefinition SharedSizeGroup="col2"></ColumnDefinition>
        </Grid.ColumnDefinition>
        <TextBox Background="Azure" Text="Hello" Grid.Column="1" MaxWidth="200" />
    </Grid>
    
    0 讨论(0)
  • 2020-12-04 15:29

    You can set HorizontalAlignment to Left, set your MaxWidth and then bind Width to the ActualWidth of the parent element:

    <Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <StackPanel Name="Container">   
        <TextBox Background="Azure" 
        Width="{Binding ElementName=Container,Path=ActualWidth}"
        Text="Hello" HorizontalAlignment="Left" MaxWidth="200" />
      </StackPanel>
    </Page>
    
    0 讨论(0)
  • 2020-12-04 15:33
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MaxWidth="200"/>
        </Grid.ColumnDefinitions>
    
        <TextBox Background="Azure" Text="Hello" />
    </Grid>
    
    0 讨论(0)
  • 2020-12-04 15:35

    Maybe I can still help somebody out who bumps into this question, because this is a very old issue.

    I needed this as well 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 so:

    <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>
    

    And finally to forget to use the System.Windows.Interactivity namespace to use the behavior.

    0 讨论(0)
  • 2020-12-04 15:40

    Both answers given worked for the problem I stated -- Thanks!

    In my real application though, I was trying to constrain a panel inside of a ScrollViewer and Kent's method didn't handle that very well for some reason I didn't bother to track down. Basically the controls could expand beyond the MaxWidth setting and defeated my intent.

    Nir's technique worked well and didn't have the problem with the ScrollViewer, though there is one minor thing to watch out for. You want to be sure the right and left margins on the TextBox are set to 0 or they'll get in the way. I also changed the binding to use ViewportWidth instead of ActualWidth to avoid issues when the vertical scrollbar appeared.

    0 讨论(0)
  • 2020-12-04 15:42

    You can use this for the Width of your DataTemplate:

    Width="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}"
    

    Make sure your DataTemplate root has Margin="0" (you can use some panel as the root and set the Margin to the children of that root)

    0 讨论(0)
提交回复
热议问题