在显示时展开 C# propertygrid

发布于 2024-09-30 02:31:18 字数 67 浏览 6 评论 0原文

我有一个关于属性网格的问题。 当显示表格时,我希望展开而不是折叠一个组。 我在网上搜索了很多,但还没有找到。 任何想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清浅ˋ旧时光 2024-10-07 02:31:18

如果您想展开网格中的所有项目,这相当简单。属性网格有一个方法可以做到这一点:

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

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;
            }
        }
    }
}
怀念你的温柔 2024-10-07 02:31:18

就我个人而言,我采用了 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();

快乐编码;-)

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

Bonjour°[大白 2024-10-07 02:31:18

(重新混合上面西蒙的答案和下面埃里克的答案...)

要展开 SelectedGridItem 的所有同级

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

(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);
     }
}
╰つ倒转 2024-10-07 02:31:18

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

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

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;
            }
        }
    }        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文