c# - 如何将文本 block 动态添加到页面?

标签 c# xaml win-universal-app

我正在学习如何制作适用于 Windows 10 的通用应用程序。大多数教程都向您展示了如何使用 XAML 以及如何在单击元素时为元素分配函数,但我找不到制作方法单击按钮时会出现一个新控件。

我正在制作笔记应用程序。我已经设计了我需要的大部分东西。现在我希望每当我点击一个按钮时创建一个新的文本 block ,用户可以在其中写下他们的笔记。

    //Create a new note when clicking the add button
    private void newNoteBtn_Click(object sender, RoutedEventArgs e)
    {

        TextBox newNote = new TextBox();
        newNote.Text = "Enter your text";
    }

这是单击按钮时运行的代码。当我运行应用程序时,什么也没有发生。我想我必须将新文本框放在某种 Grid 中什么的。

大多数教程都非常陈旧,或者提到 Windows 窗体并使用某种 this.Controls.Add(newNote);但是 Visual Studio 没有给我 Controls.Add 选项。我还创建了一个 <Grid x:Name="notes"></Grid>我认为我可以将其用作正在创建的笔记的占位符,但我无法访问 Grid通过代码元素。

最佳答案

Grid 这样的容器控件有 Children属性,所以你应该像这样使用 Childern:

TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
notes.Childern.Add(newNote);

关于c# - 如何将文本 block 动态添加到页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33974337/

相关文章:

c# - 使用 ControlTemplate 覆盖 XAML WPF 中的按钮不显示内容

c# - 互斥体不再工作

c# - 如何拦截AutoSuggestBox中的回车键?

c# - 如何仅在 XAML 中设置上边距?

c# - 使用 ExpressionVisitor 获取所有 'where' 调用

c# - Avalon UI 绑定(bind)错误 "Could not find CLR property"

c# - 如何在 C# 中将 RepeatBehavior.Forever 转换为 2x?

c# - 在 XAML 中将 ListBox 绑定(bind)到列表(集合)

c# - 从代码 (C#) 使用 FontIcon Glyph

c# - 在通用 Windows 平台中播放声音