winforms PropertyGrid 中的类别层次结构

发布于 2025-01-07 07:15:30 字数 590 浏览 0 评论 0原文

我有一些可以分为类别和子类别的属性。当我将它们添加到 PropertyGrid 时,所有子类别都位于根目录中,但我希望子类别成为类别的子类别。

class Settings
    {
        [Category("SubCategory1")]
        public bool Property1 { get; set; }

        [Category("SubCategory1")]
        public bool Property2 { get; set; }

        [Category("SubCategory2")]
        public bool Property3 { get; set; }
    }

PropertyGrid grid = new PropertyGrid();    
grid.SelectedObject = new Settings();

我想让它看起来像

 -Category1
   -SubCategory1
      Property1
      Property2
   -SubCategory2
      Property3

I have some properties that could be divided in categories and sub categories. When I add them to the PropertyGrid all subcategories are in the root, but i want sub categories to be children of categories.

class Settings
    {
        [Category("SubCategory1")]
        public bool Property1 { get; set; }

        [Category("SubCategory1")]
        public bool Property2 { get; set; }

        [Category("SubCategory2")]
        public bool Property3 { get; set; }
    }

PropertyGrid grid = new PropertyGrid();    
grid.SelectedObject = new Settings();

I want to make it look like

 -Category1
   -SubCategory1
      Property1
      Property2
   -SubCategory2
      Property3

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

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

发布评论

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

评论(1

御弟哥哥 2025-01-14 07:15:30

我找到了解决办法。方法是使用 TypeConverter 属性和嵌套类。

var settings = new Settings();
settings.Subcategory1 = new SubCategory1(){Property1 = "P1", Property2 = "P2"};
settings.Subcategory2 = new SubCategory2(){Property3="P3"};
grid.SelectedObject = settings;

...

public class Settings
{
    [Category("Category1")]
    public SubCategory1 Subcategory1 { get; set; }

    [Category("Category1")]
    public SubCategory2 Subcategory2 { get; set; }
}

[TypeConverter(typeof (ExpandableObjectConverter))]
public class SubCategory1
{
    public String Property1 { get; set; }

    public String Property2 { get; set; }

    public override string ToString()
    {
        return String.Empty;
    }
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class SubCategory2
{
    public String Property3 { get; set; }

    public override string ToString()
    {
        return String.Empty;
    }
}

希望它能帮助别人。

I have found a solution. The approach is to use TypeConverter Attribute and nested classes.

var settings = new Settings();
settings.Subcategory1 = new SubCategory1(){Property1 = "P1", Property2 = "P2"};
settings.Subcategory2 = new SubCategory2(){Property3="P3"};
grid.SelectedObject = settings;

...

public class Settings
{
    [Category("Category1")]
    public SubCategory1 Subcategory1 { get; set; }

    [Category("Category1")]
    public SubCategory2 Subcategory2 { get; set; }
}

[TypeConverter(typeof (ExpandableObjectConverter))]
public class SubCategory1
{
    public String Property1 { get; set; }

    public String Property2 { get; set; }

    public override string ToString()
    {
        return String.Empty;
    }
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class SubCategory2
{
    public String Property3 { get; set; }

    public override string ToString()
    {
        return String.Empty;
    }
}

Hope it will help someone.

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