Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 rather then collapsed. i have search a lot for that on the web and could not find it yet. any thoughts.

like image 962
guy Avatar asked Nov 03 '10 10:11

guy


2 Answers

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;
            }
        }
    }
}
like image 142
Simon Fischer Avatar answered Oct 06 '22 23:10

Simon Fischer


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

like image 41
Eric Ouellet Avatar answered Oct 06 '22 23:10

Eric Ouellet