我需要将 UIElements 插入到运行时才会生成的 Grid 中。更具体地说,在确定需要显示多少元素后,我需要将 UIElements 添加到我创建的 RowDefinitions 中。有没有办法像在 XAML 中一样为 C# 中的对象控制 Grid.Row 和 Grid.Column 和 Grid.RowSpan?如果我要解决这个问题,请告诉我。我不能使用 StackPanel(我正在创建一个动态手风琴面板,它与动画混淆)。
现在发生的事情是我在运行时生成了 RowDefinition 的数量并将 UIElements 添加为子元素。这是行不通的,所有的 UIElements 最终都在第一行中,彼此重叠。
这是我正在尝试的示例:
public partial class Page : UserControl
{
string[] _names = new string[] { "one", "two", "three" };
public Page()
{
InitializeComponent();
BuildGrid();
}
public void BuildGrid()
{
LayoutRoot.ShowGridLines = true;
foreach (string s in _names)
{
LayoutRoot.RowDefinitions.Add(new RowDefinition());
LayoutRoot.Children.Add(new Button());
}
}
}
谢谢!