3

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

Community
  • 1
  • 1
Dante
  • 3,208
  • 9
  • 38
  • 56

2 Answers2

0

Remove TextBox border and put it into ScrollViewer.

zduny
  • 2,481
  • 1
  • 27
  • 49
  • Thank you for your reply, but have tried your solution and It does not work either, thank you anyway – Dante May 16 '12 at 22:33
  • Manually change TextBox size to values remembered on resize event (I don't exactly know what you mean by "when the user stretches it") on TextChanged event? – zduny May 16 '12 at 22:42
  • You could also force TextBox size with defining some grid rows and columns. – zduny May 16 '12 at 22:48
  • Sorry, I meant "when user stretches the window that contains the TextBox" – Dante May 16 '12 at 22:50
0

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>
Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
  • Thank you for your reply. I just tried it, but I did not work either, It still behaves the same way: I have to stretch the window in order to stop my textbox from growing – Dante May 17 '12 at 16:01