WPF - 垂直增长以容纳所有文本的文本框

发布于 2024-08-12 07:30:08 字数 798 浏览 4 评论 0原文

问题:我没有得到具有水平自动换行和垂直自动增长功能的文本框设置。我希望通过编写代码来做到这一点。我编写了以下代码,在鼠标双击时使用自动换行创建一个文本框:

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪之魂 2024-08-19 07:30:08

尝试使用此

        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;

窗口,其中 window 是分配给 Window(root) 的名称。

try using this

        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 Window(root).

巷子口的你 2024-08-19 07:30:08

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.

下壹個目標 2024-08-19 07:30:08

你试过这个吗?

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

而不是

text2.Height = 30;

不设置它或使用 double.NaN 与在 xaml 中使用“Auto”相同。

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 of

text2.Height = 30;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文