在show上展开C#propertygrid

时间:2010-11-03 10:31:08

标签: c# propertygrid

我对房产网格有疑问。 当表单显示时,我希望一个组扩展而不是折叠。 我在网上搜索了很多,但还没找到。 任何想法。

4 个答案:

答案 0 :(得分:13)

如果要扩展网格中的所有项目,这非常简单。属性网格有一个方法:

propertyGrid.ExpandAllGridItems();

如果您想扩展某个群组,可以使用此方法:

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

答案 1 :(得分:8)

就个人而言,我采用了Simon的答案,并使用它创建了一个扩展,并添加了面向方面编程技术,使用属性声明扩展对象(如果需要,可以添加你的味道,这很容易):

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

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

这个班级

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

用法是:

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

电话是:

this.propertyGrid.ExpandItemWithInitialExpandedAttribute();

快乐编码; - )

答案 2 :(得分:2)

(重新混合Simon上面的回答以及Eric的回答......)

展开 SelectedGridItem的所有兄弟

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

答案 3 :(得分:0)

这些枚举器扩展允许我做我想做的一切。

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