Expand C# propertygrid on show

此生再无相见时 提交于 2019-11-28 11:33:31

If you want to expand all items in the grid it is fairly simple. The property grid has a method for doing that:

propertyGrid.ExpandAllGridItems();

If it is a certain group you want to expand you can use this method:

private static void ExpandGroup(PropertyGrid propertyGrid, string groupName)
{
    GridItem root = propertyGrid.SelectedGridItem;
    //Get the parent
    while (root.Parent != null)
        root = root.Parent;

    if (root != null)
    {
        foreach (GridItem g in root.GridItems)
        {
            if (g.GridItemType == GridItemType.Category && g.Label == groupName)
            {
                g.Expanded = true;
                break;
            }
        }
    }
}
Eric Ouellet

Personally, I took Simon's answer and created an extension with it and added the Aspect Oriented Programming technique of declaring an expanded object using Attributes (you can add your flavor if you want, it's easy):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HQ.Util.WinFormUtil
{
    public static class PropertyGridExtension
    {
        // ******************************************************************
        public static void ExpandGroupName(this PropertyGrid propertyGrid, string groupName)
        {
            foreach (GridItem gridItem in propertyGrid.SelectedGridItem.GridItems)
            {
                if (gridItem != null)
                {
                    if (gridItem.GridItemType == GridItemType.Category && gridItem.Label == groupName)
                    {
                        gridItem.Expanded = true;
                    }
                }
            }
        }

        // ******************************************************************
        public static void ExpandItemWithInitialExpandedAttribute(this PropertyGrid propertyGrid)
        {
            ExpandItemWithInitialExpandedAttribute(propertyGrid, propertyGrid.SelectedGridItem);
        }

        // ******************************************************************
        private static void ExpandItemWithInitialExpandedAttribute(PropertyGrid propertyGrid, GridItem gridItem)
        {
            if (gridItem != null)
            {
                if (gridItem.GridItemType == GridItemType.Property && gridItem.Expandable)
                {
                    object[] objs = gridItem.Value.GetType().GetCustomAttributes(typeof(PropertyGridInitialExpandedAttribute), false);
                    if (objs.Length > 0)
                    {
                        if (((PropertyGridInitialExpandedAttribute) objs[0]).InitialExpanded)
                        {
                            gridItem.Expanded = true;
                        }
                    }
                }

                foreach (GridItem childItem in gridItem.GridItems)
                {
                    ExpandItemWithInitialExpandedAttribute(propertyGrid, childItem);
                }
            }
        }

        // ******************************************************************
    }
}

And this class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HQ.Util.WinFormUtil
{
    public class PropertyGridInitialExpandedAttribute : Attribute
    {
        public bool InitialExpanded { get; set; }

        public PropertyGridInitialExpandedAttribute(bool initialExpanded)
        {
            InitialExpanded = initialExpanded;
        }
    }
}

And the usage is:

[PropertyGridInitialExpanded(true)]
public class YourClass
{
    ...
}

And the call is:

this.propertyGrid.ExpandItemWithInitialExpandedAttribute();

Happy coding ;-)

(Re-mixing Simon's answer above and Eric's answer below...)

To expand all of the siblings of the SelectedGridItem:

public static void ExpandItemWithInitialExpandedAttribute(this PropertyGrid propertyGrid)
{
     foreach (GridItem item in propertyGrid.SelectedGridItem.Parent.GridItems)
     {
         ExpandItemWithInitialExpandedAttribute(propertyGrid, item);
     }
}

These enumerator extensions allowed me to do everything I wanted.

public static class PropertyGridExtensions
{
    public static IEnumerable<GridItem> EnumerateGroups(this PropertyGrid propertyGrid)
    {
        if (propertyGrid.SelectedGridItem == null)
            yield break;

        foreach (var i in propertyGrid.EnumerateItems())
        {
            if (i.Expandable)
            {
                yield return i;
            }
        }
    }

    public static IEnumerable<GridItem> EnumerateItems(this PropertyGrid propertyGrid)
    {
        if (propertyGrid.SelectedGridItem == null)
            yield break;

        var root = propertyGrid.SelectedGridItem;
        while (root.Parent != null)
            root = root.Parent;

        yield return root;

        foreach (var i in root.EnumerateItems())
        {
            yield return i;
        }            
    }

    public static GridItem GetGroup(this PropertyGrid propertyGrid, string label)
    {
        if (propertyGrid.SelectedGridItem == null)
            return null;

        foreach (var i in propertyGrid.EnumerateItems())
        {
            if (i.Expandable && i.Label == label)
            {
                return i;
            }
        }

        return null;
    }

    private static IEnumerable<GridItem> EnumerateItems(this GridItem item)
    {
        foreach (GridItem i in item.GridItems)
        {
            yield return i;

            foreach (var j in i.EnumerateItems())
            {
                yield return j;
            }
        }
    }        
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!