WPF is a text box that grows vertically to accommodate all text.

Problem: I do not get a text box setting that will have a horizontal wordwrap function and vertically automatically increase functionality. I want to do this by writing code. I wrote the following code that creates a dblclick mouse text field with wordwrap:

TextBox text2 = new TextBox(); text2.Width = 500; text2.Visibility = Visibility.Visible; text2.Focus(); text2.Height = 30; text2.HorizontalAlignment = HorizontalAlignment.Left; text2.VerticalAlignment = VerticalAlignment.Top; Point p = e.GetPosition(LayoutRoot); text2.Margin = new Thickness(pX, pY, 0, 0); LayoutRoot.Children.Add(text2); 

But the text box does not grow vertically. Can someone suggest me a C # code to do exactly what I want?

+4
source share
3 answers

try using

  Grid grid = new Grid(); grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); grid.RowDefinitions.Add(new RowDefinition()); TextBox textBox = new TextBox() { Width = 100, TextWrapping = TextWrapping.Wrap }; textBox.SetValue(Grid.RowProperty, 0); grid.Children.Add(textBox); window.Content = grid; 

where window is the Name assigned to the window (root).

+5
source

Have you tried this?

 text2.Height = double.NaN; // or don't set the property, but some custom styles might give a default value .. text2.TextWrapping = TextWrapping.Wrap; text2.MinHeight = 30; // or not if you want the styles default 

instead

 text2.Height = 30; 

not setting it or using double.NaN is the same as using "Auto" in xaml.

0
source

All Articles