在动态创建的文本框的代码隐藏中将文本框的高度设置为自动

编程入门 行业动态 更新时间:2024-10-24 20:20:43
本文介绍了在动态创建的文本框的代码隐藏中将文本框的高度设置为自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在通过代码动态创建一个文本框,并将其添加到 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 14:34:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   设置为   中将   高度   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!