将行/列动态添加到网格 [英] Add Rows/Columns to a Grid dynamically
问题描述
我有一个孩子 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
size(列/行计数),可以使用助手,如@Rachel' s WPF网格的行/列计数属性
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.
这篇关于将行/列动态添加到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!