【发布时间】:2021-02-05 14:44:00
【问题描述】:
我根据用户输入生成行和列,但我在后面的代码中执行此操作。但后来我想使用 XAML 添加按钮或文本块等元素。有什么建议?或者有什么方法可以使用 XAML 生成行和列?
Grid grid = new Grid();
grid.ShowGridLines = true;
for (int i = 0; i < 5; i++)
{
ColumnDefinition column = new ColumnDefinition();
RowDefinition row = new RowDefinition();
grid.RowDefinitions.Add(row);
grid.ColumnDefinitions.Add(column);
}
this.Content = grid;
然后我想添加一个这样的按钮:
<Button Grid.Column="3" Grid.Row="1">
</Button>
【问题讨论】:
-
我很难理解您的要求。
-
推荐的动态构建 ui 的方法是将 xaml 构建为字符串,然后 xamlreader.parse 将其转换为控件。 social.technet.microsoft.com/wiki/contents/articles/…
标签: c# wpf xaml code-behind