动态地将行/列添加到网格 [英] Add Rows/Columns to a Grid dynamically

查看:24
本文介绍了动态地将行/列添加到网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个文本框、标签、图像等的子 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.

这篇关于动态地将行/列添加到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆