c# - 如何在 PropertyGrid 中显示带有子类的对象

标签 c# propertygrid

我将使用 PropertyGrid 来显示我的对象。这是信息类。 Info 类具有一些由类类型组成的属性。但是,子类不显示属性。你有什么主意吗?

代码片段:

using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication_propertyGrid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            info _in = new info();
            this.propertyGrid1.SelectedObject = _in;
        }
    }

    [DefaultPropertyAttribute("Name")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class info
    {
        private int _id;
        [CategoryAttribute("Defaults")]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        [CategoryAttribute("Defaults")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private DoublePoint _resultMarkPos;
        [CategoryAttribute("Results")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public DoublePoint ResultMarkPos
        {
            get { return _resultMarkPos; }
            set { _resultMarkPos = value; }
        }

        public struct DoublePoint
        {
            public double x, y;
        }

        private subInfo1 _sub1;
        [CategoryAttribute("SubInfo")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public subInfo1 SubInfo1
        {
            get { return _sub1; }
            set { _sub1 = value; }
        }

        private subInfo2 _sub2;
        [CategoryAttribute("SubInfo2")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public subInfo2 SubInfo2
        {
            get { return _sub2; }
            set { _sub2 = value; }
        }

        public info()
        {
            this._id = 0;
            this._name = "info";

            this._resultMarkPos.x = 0;
            this._resultMarkPos.y = 0;

            this._sub1 = new subInfo1
            {
                Id = 11,
                Name = "sub11",
            };

            this._sub2 = new subInfo2
            {
                Id = 22,
                Name = "sub22",
            };
        }
    }

    public class subInfo1
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public subInfo1()
        {
            this._id = 0;
            this._name = "sub1";
        }
    }

    public class subInfo2
    {
        private int _id;
        public int Id 
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public subInfo2()
        {
            this._id = 0;
            this._name = "sub2";
        }
    }
}

已编辑 但是,struct case 不影响 [TypeConverter(typeof(ExpandableObjectConverter))] 属性。你有什么主意吗 ?

private DoublePoint _resultMarkPos;
[CategoryAttribute("Results")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public DoublePoint ResultMarkPos
{
            get { return _resultMarkPos; }
            set { _resultMarkPos = value; }
}

public struct DoublePoint
{
        public double x, y;
}

最佳答案

你需要使用TypeConverter:

    private subInfo1 _sub1;        
    [CategoryAttribute("SubInfo")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public subInfo1 SubInfo1
    {
        get { return _sub1; }
        set { _sub1 = value; }
    }
    private subInfo2 _sub2;
    [CategoryAttribute("SubInfo2")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public subInfo2 SubInfo2
    {
        get { return _sub2; }
        set { _sub2 = value; }
    }

关于c# - 如何在 PropertyGrid 中显示带有子类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18546919/

相关文章:

c# - 使用 C# 创建编程语言

c# - 使属性在 DataGridView 中可见但在 PropertyGrid 中不可见?

C# PropertyGrid 拖放

c# - 使用 GroupBy 和 Average 将 SQL 转换为 lambda LINQ

c# - 了解一些 C# 代码

c# - 尝试包装 2 个 Polly 策略时出现转换错误

c# - 添加错误处理以从数据库中删除用户

c# - 具有继承类的属性列表的 PropertyGrid 编辑器

c# - PropertyGrid 替代品

c# - 如何更改 PropertyGrid 控件的边框颜色(或删除边框)?