开发者

Expand C# propertygrid on show

i have a question about property grid. when the form is shown i would like a group to be expand rat开发者_如何学Pythonher then collapsed. i have search a lot for that on the web and could not find it yet. any thoughts.


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;
            }
        }
    }
}


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;
            }
        }
    }        
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜