TextBox expanding with surrounding Grid but not with text

前端 未结 3 787
我寻月下人不归
我寻月下人不归 2021-02-20 09:21

A window has a Grid with two columns. The left column contains a control with a constant width but with a height that adapts. The right column contains a TextBox that takes up a

相关标签:
3条回答
  • 2021-02-20 09:56

    You could use an invisible border (its hacky but it works - its how I tend to sort out dynamic textbox sizes):

    <Border BorderThickness="0" x:Name="border" Grid.Column="1" Margin="0.5" />
                    <TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}" />
    
    0 讨论(0)
  • 2021-02-20 10:04

    Have you tried setting the MaxWidth property on just the TextBox?

    Edit after OP's comment

    I would try getting rid of the ScrollViewer. The sizing used in the Grid's layout should take care of re-sizing and the scroll bar settings on the TextBox should take care of the rest.

    0 讨论(0)
  • 2021-02-20 10:10

    The answer is based on Leom's answer.

    The solution works great when you enlarge the window, but the resizing is not smooth when you make the window smaller. As the textbox participates in the grid's layout, it has to perform layout process multiple times. You can fix that by putting the texbox in the canvas, so the change of the size of the textbox no longer triggers the grid's re-layout.

    The updated code:

    <Border BorderThickness="0" x:Name="border" Grid.Column="1" Margin="0.5" />
    <Canvas Grid.Column="1">
        <TextBox AcceptsReturn="True" TextWrapping="Wrap" Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}" />
    </Canvas>
    
    0 讨论(0)
提交回复
热议问题