Stop WPF TextBox from growing

2019-05-24 11:36发布

I have spent two hours researching how to avoid that my WPF TextBox Control grows when a long text has been typed in, but I have not been able to do it, even I have read some answers about it like these ones:

stopping-wpf-textbox-from-growing-with-text

wpf-allow-textbox-to-be-resized-but-not-to-grow-on-user-input

wpf-how-to-stop-textbox-from-autosizing

My code is the following:

<Grid>
      <TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" AcceptsReturn="True"/>
      <CheckBox Content="Case sensitive" HorizontalAlignment="Left" Margin="7,0,0,2" Name="checkBox1" Height="16" VerticalAlignment="Bottom" />
</Grid>

One thing that I tried was:

MaxWidth={Binding ElementName=MyGrid, Path=ActualWidth} 

But it did not work for me.

I also tried to add the following property to the Grid:

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

Or

<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" ....... />

But it did not work either.

I need my control to grow when the user stretches the window, but not to when a long text is inserted.

Do you have a different answer for this problem that I might try?

UPDATE!

I have noticed something very strange: If I stretch manually the window, the textbox stops growing when a long text is inserted. That's fine. But I need to achieve this without having to stretch the window every time I run the program

标签: wpf textbox
2条回答
三岁会撩人
2楼-- · 2019-05-24 11:54

Try to do next:

<ScrollViewer MaxHeight={Binding ElementName=MyGrid, Path=ActualWidth} BorderThickness="0">                    
      <TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" AcceptsReturn="True"/>          
</ScrollViewer>
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-05-24 12:11

Remove TextBox border and put it into ScrollViewer.

查看更多
登录 后发表回答