wpf 动态添加行/列到网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18296889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Rows/Columns to a Grid dynamically
提问by WAQ
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.
我有一个UserControl
由多个文本框、标签、图像等组成的子项。我需要在具有网格的父用户控件中填充此用户控件。
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.
需要在父用户控件中填充的子用户控件的数量在运行时确定并且可以更改。基本上数量取决于搜索结果。所以它可以是从 0 到 n。
How can I populate my child user control in parent control? Or is there a better alternate to using a Grid control for this?
如何在父控件中填充我的子用户控件?或者是否有更好的替代方法来为此使用 Grid 控件?
Note: I have to do this in my ViewModel.
注意:我必须在我的 ViewModel 中执行此操作。
回答by Viv
To have a dynamic Grid
size(column/row count), you can use helpers such as @Rachel 's WPF Grid's Row/Column Count Properties
要获得动态Grid
大小(列/行数),您可以使用诸如 @Rachel 的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:
在您的情况下,这取决于您在大多数基于上下文的简单情况下尝试实现的目标,当有以下要求时:
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.
需要在父用户控件中填充的子用户控件的数量在运行时确定并且可以更改。基本上数量取决于搜索结果。所以它可以是从 0 到 n。
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 ItemsPanelto control layout as you see fit.
我更喜欢使用ItemsControl( Example)。使用ItemTemplate
链接到您孩子UserControl
的 。如果需要,您还可以根据需要调整ItemsPanel以控制布局。