【发布时间】:2014-10-02 07:23:44
【问题描述】:
我正在尝试以编程方式在我的网格中添加TextBox。
这是我的代码:
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
var input= await OneListDataSource.GetItemAsync((string)e.NavigationParameter);
foreach (string item in input.getItems()) {
TextBox textBox = new TextBox();
textBox.Text = item;
grid.RowDefinitions.Add(new RowDefinition());
grid.Children.Add(textBox);
}
}
在我的 xaml 中:
<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0">
</Grid>
但是当我在模拟器中运行它时,我只看到 1 行并且所有项目似乎相互重叠。
我尝试在我的 xaml 文件中删除 Grid.Row,但我认为它没有任何区别。
更新:
感谢您的回答,我几乎可以正常工作了。现在我的问题是 为什么我需要添加一个新的 RowDefintion() 一切我添加一个新的 children(),我可以在没有 RowDefinitions.Add(new RowDefinition()) 的情况下为每一行添加吗?
foreach (string item in input.getItems()) {
TextBox textBox = new TextBox();
textBox.Text = item;
grid.RowDefinitions.Add(new RowDefinition()); // why I need to do this everytime I add a new TextBox?
Grid.SetRow(textBox, i++);
Grid.SetColumn(textBox, 0);
grid.Children.Add(textBox);
}
【问题讨论】:
-
... 或者this。
-
如果你只是想显示一个文本框列表,不如使用 stackpanel 来代替?
-
或者更好,一个项目控制
-
我研究项目控制。如何以编程方式将 TextBox 添加到 itemsControl?