Stop WPF TextBox from growing

I spent two hours learning how to avoid my WPF TextBox Control growing when long text is typed, but I was unable to do so, even if I read some answers about it:

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 as follows:

<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 I've tried:

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

      

But it didn't 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 that didn't work either.

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

Do you have another answer to this problem that I can try?

UPDATE!

I noticed something very strange: if I manually expand the window, the textbox stops growing when long text is inserted. It's great. But I need to achieve this without stretching the window every time I run the program

+2


source to share


2 answers


Remove the border of the TextBox and place it in the ScrollViewer.



0


source


Try the following:



<ScrollViewer MaxHeight={Binding ElementName=MyGrid, Path=ActualWidth} BorderThickness="0">                    
      <TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" AcceptsReturn="True"/>          
</ScrollViewer>

      

0


source







All Articles