Expand C# propertygrid on show
Asked Answered
W

4

7

i have a question about property grid. when the form is shown i would like a group to be expand rather then collapsed. i have search a lot for that on the web and could not find it yet. any thoughts.

Willianwillie answered 3/11, 2010 at 10:31 Comment(0)
C
18

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;
            }
        }
    }
}
Collett answered 3/11, 2010 at 12:22 Comment(1)
thank. it just as i did it at the end. great mind think a likeWillianwillie
H
9

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

Harpsichord answered 29/4, 2011 at 19:41 Comment(0)
C
2

(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);
     }
}
Cohn answered 30/10, 2015 at 16:23 Comment(0)
O
1

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;
            }
        }
    }        
}
Obbligato answered 10/6, 2014 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.