Silverlight 2 中使用 C# 的动态网格
我需要将 UIElement 插入到运行时才生成的网格中。 更具体地说,在确定需要显示多少个元素后,我需要将 UIElements 添加到我创建的 RowDefinitions 中。 有没有办法像 C# 中的对象的 XAML 一样控制 Grid.Row、Grid.Column 和 Grid.RowSpan? 如果我的做法是错误的,请告诉我。 我无法使用 StackPanel(我正在创建一个动态手风琴面板,它与动画混淆)。
现在发生的情况是,我在运行时生成 RowDefinition 的数量,并将 UIElement 添加为子元素。 这是行不通的,所有 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());
}
}
}
谢谢!
I need to insert UIElements into a Grid that does not get generated until runtime. More specifically, I need to add UIElements to the RowDefinitions I create after I determine how many elements need to be displayed. Is there a way to contorl the Grid.Row and Grid.Column and Grid.RowSpan like in XAML for objects in C#? If I am going about this wrong, please let me know. I can not use a StackPanel (I am creating an dynamic accordian panel and it messes with the animation).
Right now what happens is that I generate the number of RowDefinitions at runtime and add UIElements as the children. This isn't working, all the UIElements end up in the first row layered on top of each other.
Here is an example of what I am trying:
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());
}
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了网格并不是真正适合这项工作的工具(ListView 是)之外,您还需要告诉按钮它属于哪一行。
Apart from the Grid not really being the right tool for the job (a ListView would be) you need to tell the Button which row it belongs to.
执行您要查找的操作的最佳方法是遵循以下模式:
Page.xaml:
Page.xaml.cs:
这会根据字符串数组的内容动态构建行。 将来更好的方法是使用 ObservableCollection< /a> 其中 T 实现 INotifyPropertyChanged。 如果您从集合中删除或添加项目以及 T 的属性发生更改,这将通知 DataGrid 更新其行。
要进一步自定义用于显示内容的 UIElements,您可以使用 DataGridTemplateColumn:
The best way to do what you're looking for is to follow the pattern below:
Page.xaml:
Page.xaml.cs:
This builds the rows dynamically from the contents of your string array. In future an even better way would be to use an ObservableCollection where T implements INotifyPropertyChanged. This will notify the DataGrid to update it's rows if you remove or add an item from the collection and also when properties of T change.
To further customize the UIElements used to display things you can use a DataGridTemplateColumn:
我从未使用过这些东西,但是您不应该将 Button 添加到 RowDefinitions 而不是 Children 中吗? (只是一个逻辑观察)
I have never used this stuff, but shouldn't you be adding the Button to the RowDefinitions, instead of the Children? (just a logical observation)