我有一个包含多个文本框、标签、图像等的子 UserControl.我需要将此用户控件填充到具有网格的父用户控件中.
I have a child UserControl comprising of multiple textboxes, labels, image etc. I need to populate this user control in a Parent User control which has a Grid.
需要在父用户控件中填充的子用户控件的数量在运行时确定并且可以更改.基本上这个数字取决于搜索结果.所以它可以是从0到n.
The number of child user controls that need to be populated in the parent user control are determined at run time and can change. Basically the number depends on the search result. So it can be from 0 to n.
如何在父控件中填充我的子用户控件?或者有没有更好的替代方法来使用 Grid 控件?
How can I populate my child user control in parent control? Or is there a better alternate to using a Grid control for this?
注意:我必须在我的 ViewModel 中执行此操作.
Note: I have to do this in my ViewModel.
推荐答案要拥有动态的 Grid 大小(列/行数),您可以使用诸如 @Rachel 的 WPF Grid 的行/列计数属性
To have a dynamic Grid size(column/row count), you can use helpers such as @Rachel 's WPF Grid’s Row/Column Count Properties
在您的情况下,这取决于您在大多数基于上下文的简单情况下尝试实现的目标,当有以下要求时:
In your case, this depends on what exactly your trying to achieve as in most simple cases based on context, when there is a requirement such as:
需要在父用户控件中填充的子用户控件的数量在运行时确定并且可以更改.基本上这个数字取决于搜索结果.所以它可以是从0到n.
Number of child user controls that need to be populated in the parent user control are determined at run time and can change. Basically the number depends on the search result. So it can be from 0 to n.
我更喜欢使用 ItemsControl(示例).使用 ItemTemplate 链接到您的孩子 UserControl 的.如果需要,您还可以调整 ItemsPanel 控制您认为合适的布局.
I'd prefer to go with an ItemsControl (Example). Use the ItemTemplate to link to your child UserControl's. If needed you can also tweak the ItemsPanel to control layout as you see fit.
更多推荐
动态地将行/列添加到网格
发布评论