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

前端 未结 8 2271
隐瞒了意图╮
隐瞒了意图╮ 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:46

    Functionally similar to the accepted answer, but doesn't require the parent element to be specified:

    <TextBox
        Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}}"
        MaxWidth="500"
        HorizontalAlignment="Left" />
    
    0 讨论(0)
  • 2020-12-04 15:48

    In my case I had to put textbox into a stack panel in order to stretch textbox on left side. Thanks to previous post. Just for an example I did set a background color to see what’s happens when window size is changing.

    <StackPanel Name="JustContainer" VerticalAlignment="Center" HorizontalAlignment="Stretch" Background="BlueViolet" >
        <TextBox 
           Name="Input" Text="Hello World" 
           MaxWidth="300"
           HorizontalAlignment="Right"
           Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FrameworkElement}}}">
        </TextBox>
    </StackPanel>
    
    0 讨论(0)
提交回复
热议问题