0
我想在我的网格中编程添加TextBox
。如何动态地将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(新的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);
}
可能的重复的[WPF - 在代码隐藏的网格中更改控件的位置](http://stackoverflow.com/questions/3785572/wpf-change-the-place-of-a-control-在网格中的代码后面) – 2014-10-01 22:10:29
...或者[this](http://stackoverflow.com/questions/7127125/how-to-add-wpf-control-to-particular-grid-行和细胞过程中的运行时)。 – 2014-10-01 22:10:51
如果你只是想显示一个文本框列表,那么用堆栈面板代替它怎么样? – failedprogramming 2014-10-01 22:11:19