WPF - 垂直增长以容纳所有文本的文本框
问题:我没有得到具有水平自动换行和垂直自动增长功能的文本框设置。我希望通过编写代码来做到这一点。我编写了以下代码,在鼠标双击时使用自动换行创建一个文本框:
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(p.X, p.Y, 0, 0);
LayoutRoot.Children.Add(text2);
但是,文本框不会垂直增长。 有人可以建议我用 C# 编写代码来完成我想要的事情吗?
The problem: I am not getting a textbox setting that will have a horizontally wordwrap and vertically auto grow functionality. I wish to do that by writing a code. I have written following code that creates a text box at mouse dblclick 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(p.X, p.Y, 0, 0);
LayoutRoot.Children.Add(text2);
But, textbox does not grow vertically.
Can somebody suggest me a code in C# to do exactly what I desire?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用此
窗口,其中 window 是分配给 Window(root) 的名称。
try using this
where window is the Name assigned to Window(root).
实现所需增长的一种方法是使用字符串测量机制 每当文本框中的文本发生变化时,您都可以运行它。只需根据内容的任何更改相应地测量文本框并调整其大小即可。
One way to accomplish the growth you're looking for is to use a string measuring mechanism which you would run any time the text in your text box changes. Simply measure and resize your text box accordingly with any change to the contents.
你试过这个吗?
而不是
不设置它或使用 double.NaN 与在 xaml 中使用“Auto”相同。
Have you tried this?
instead of
not setting it or using double.NaN is the same as using 'Auto' in xaml.