如何将文本块动态添加到页面?
How do I add a textblock dynamically to a page?
我正在学习如何为 windows 10 制作通用应用程序。大多数教程都向您展示了如何使用 XAML 以及如何在单击元素时将功能分配给元素但是当我单击按钮时,我找不到使新控件出现的方法。
我正在制作笔记应用程序。我已经设计了我需要的大部分东西。现在我希望每当我单击一个按钮时创建一个新的文本块,用户可以在其中写下他们的笔记。
//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);
定义时
<Grid x:Name="notes"></Grid>
在页面的 XAML 中,您可以使用 notes
作为标识符从页面后面的代码访问此 Grid
:
notes.Children.Add(newNote);
我正在学习如何为 windows 10 制作通用应用程序。大多数教程都向您展示了如何使用 XAML 以及如何在单击元素时将功能分配给元素但是当我单击按钮时,我找不到使新控件出现的方法。
我正在制作笔记应用程序。我已经设计了我需要的大部分东西。现在我希望每当我单击一个按钮时创建一个新的文本块,用户可以在其中写下他们的笔记。
//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);
定义时
<Grid x:Name="notes"></Grid>
在页面的 XAML 中,您可以使用 notes
作为标识符从页面后面的代码访问此 Grid
:
notes.Children.Add(newNote);