如何枚举PropertyGrid项目?
我有一个 PropertyGrid
并为其分配了一些对象。
var prpGrid = new PropertyGrid();
prp.SelectedObject = myObject;
我想获取所有网格项,就像我可以获取 selectedGridItem 属性一样:
var selectedProperty = prpGrid.SelectedGridItem;
我可以这样做吗?
I have a PropertyGrid
with assigned to it some object.
var prpGrid = new PropertyGrid();
prp.SelectedObject = myObject;
I want to get all grid items like I can get selectedGridItem property:
var selectedProperty = prpGrid.SelectedGridItem;
Can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面是一段代码,它将检索属性网格的所有 GridItem 对象:
当然,由于这是使用属性网格的未记录的私有字段,因此不能保证将来可以工作:-)
一旦您拥有了所有 GridItems ,您可以使用 GridItem.GridItemType 属性来过滤它们。
Here is a piece of code that will retrieve all GridItem objects of a property grid:
Of course, since this is using an undocumented private field of the Property Grid, is not guaranteed to work in the future :-)
Once you have all the GridItems, you can filter them using the GridItem.GridItemType property.
如果您只需要对象的属性,则可以通过反射获取这些属性:
如果您确实使用
BrowsableAttribute(false)
隐藏了某些属性,则可以使用GetProperties(Type, Attribute[])
将其过滤掉。我不知道返回 GridItem 集合的方法。
更新
当然你也可以通过Reflection获取PropertyGrid用于标签的字符串。
如果您确实使用
DisplayNameAttribute("ABC")
修饰该属性,则应该能够通过GetCustomAttributes(类型,布尔值)
。否则只需使用 PropertyDescriptor 的名称。If you only need the object's properties, you can get those via Reflection:
If you did hide some of the properties with
BrowsableAttribute(false)
, you can useGetProperties(Type, Attribute[])
to filter those out.I am not aware of a method that returns a GridItem collection.
Update
Of course you can also obtain the string that the PropertyGrid uses for the labels via Reflection.
If you did decorate the property with
DisplayNameAttribute("ABC")
, you should be able to access DisplayName viaGetCustomAttributes(Type, Boolean)
. Otherwise just use the Name of the PropertyDescriptor.我知道这是一个老问题,但我刚刚遇到了同样的问题并使用此代码解决了它(假设
PropertyGrid
变量称为grid
):当然,这个例子可以与只有一级类别的简单属性网格一起使用。
I know this is an old question, but I have just encountered the same issue and solved it using this code (suppose
PropertyGrid
variable is calledgrid
):Of course, this example can be used with simple property grid which has only one level of categories.