在动态创建的文本框的代码隐藏中将文本框的高度设置为自动 [英] Setting height of textbox to auto in code-behind for a dynamically created textbox

查看:31
本文介绍了在动态创建的文本框的代码隐藏中将文本框的高度设置为自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过代码动态创建一个文本框,并将其添加到 LayoutRoot.我希望文本框支持多行,因此我将 AcceptsReturn 属性设置为 true 并将 TextWrapping 属性设置为 Wrap.我在另一个问题中读到要将 Height 设置为 Auto,我们必须使用 double.NaN,我已经这样做了.但是,当我添加它时,它的高度是无限的,并且覆盖了整个空间.我只希望文本框最初是一行,并在添加行时让它增加高度.请帮我解决这个问题.

I am creating a textbox dynamically via code, and adding it to the LayoutRoot. I want the textbox to support multiline, so I have set the AcceptsReturn property to true and TextWrapping property to Wrap. I read in another question that to set the Height as Auto, we have to use double.NaN, and I have done this. But, when I add it, its height is infinite, and covers up the whole space. I just want the textbox to be of a single line initially and let it increase the height when lines are added to it. Please help me with this solution.

推荐答案

将您的 TextBox 包裹在 StackPanel 中.如果你通过代码来做,你可以做这样的事情,例如:

Wrap your TextBox in a StackPanel. If you're doing it via code, you could do something like this, for example:

public MainPage()
{
    InitializeComponent();

    var textBox = new TextBox
    {
        AcceptsReturn = true,
        Height = Double.NaN,
        TextWrapping = TextWrapping.Wrap
    };

    var stackPanel = new StackPanel();
    stackPanel.Children.Add(textBox);

    this.LayoutRoot.Children.Add(stackPanel);
}

这篇关于在动态创建的文本框的代码隐藏中将文本框的高度设置为自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆