如何枚举PropertyGrid项目?

发布于 2024-10-20 02:41:00 字数 281 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

々眼睛长脚气 2024-10-27 02:41:00

下面是一段代码,它将检索属性网格的所有 GridItem 对象:

public static GridItemCollection GetAllGridEntries(this PropertyGrid grid)
{
    if (grid == null)
        throw new ArgumentNullException("grid");

    var field = grid.GetType().GetField("gridView", BindingFlags.NonPublic | BindingFlags.Instance);
    if (field == null)
    {
        field = grid.GetType().GetField("_gridView", BindingFlags.NonPublic | BindingFlags.Instance);
        if (field == null)
            return null;
    }

    var view = field.GetValue(grid);
    if (view == null)
        return null;

    try
    {
        return (GridItemCollection)view.GetType().InvokeMember("GetAllGridEntries", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, view, null);
    }
    catch
    {
        return null;
    }
}

当然,由于这是使用属性网格的未记录的私有字段,因此不能保证将来可以工作:-)

一旦您拥有了所有 GridItems ,您可以使用 GridItem.GridItemType 属性来过滤它们。

Here is a piece of code that will retrieve all GridItem objects of a property grid:

public static GridItemCollection GetAllGridEntries(this PropertyGrid grid)
{
    if (grid == null)
        throw new ArgumentNullException("grid");

    var field = grid.GetType().GetField("gridView", BindingFlags.NonPublic | BindingFlags.Instance);
    if (field == null)
    {
        field = grid.GetType().GetField("_gridView", BindingFlags.NonPublic | BindingFlags.Instance);
        if (field == null)
            return null;
    }

    var view = field.GetValue(grid);
    if (view == null)
        return null;

    try
    {
        return (GridItemCollection)view.GetType().InvokeMember("GetAllGridEntries", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, view, null);
    }
    catch
    {
        return null;
    }
}

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.

屋顶上的小猫咪 2024-10-27 02:41:00

如果您只需要对象的属性,则可以通过反射获取这些属性:

PropertyDescriptorCollection myObjectProperties = TypeDescriptor.GetProperties(myObject);

如果您确实使用 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:

PropertyDescriptorCollection myObjectProperties = TypeDescriptor.GetProperties(myObject);

If you did hide some of the properties with BrowsableAttribute(false), you can use GetProperties(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 via GetCustomAttributes(Type, Boolean). Otherwise just use the Name of the PropertyDescriptor.

江南烟雨〆相思醉 2024-10-27 02:41:00

我知道这是一个老问题,但我刚刚遇到了同样的问题并使用此代码解决了它(假设 PropertyGrid 变量称为 grid):

public void IteratePropertyGrid()
{
    GridItemCollection categories;
    if (grid.SelectedGridItem.GridItemType == GridItemType.Category)
    {
        categories = grid.SelectedGridItem.Parent.GridItems;
    }
    else
    {
        categories = grid.SelectedGridItem.Parent.Parent.GridItems;
    }

    foreach (var category in categories)
    {
        if (((GridItem)category).GridItemType == GridItemType.Category)
        {
            foreach (GridItem gi in ((GridItem)category).GridItems)
            {
                // Do something with gi                         
            }
        }
    }
}

当然,这个例子可以与只有一级类别的简单属性网格一起使用。

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 called grid):

public void IteratePropertyGrid()
{
    GridItemCollection categories;
    if (grid.SelectedGridItem.GridItemType == GridItemType.Category)
    {
        categories = grid.SelectedGridItem.Parent.GridItems;
    }
    else
    {
        categories = grid.SelectedGridItem.Parent.Parent.GridItems;
    }

    foreach (var category in categories)
    {
        if (((GridItem)category).GridItemType == GridItemType.Category)
        {
            foreach (GridItem gi in ((GridItem)category).GridItems)
            {
                // Do something with gi                         
            }
        }
    }
}

Of course, this example can be used with simple property grid which has only one level of categories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文