如何在PropertyGrid中显示具有子类的对象

本文关键字:子类 对象 显示 PropertyGrid | 更新日期: 2023-09-27 18:29:47

我将使用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";
        }
    }
}

已编辑但是,结构大小写对[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;
}

如何在PropertyGrid中显示具有子类的对象

您需要使用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; }
    }

您的代码很好,但您只是在类声明中忘记了一点额外信息:public class MyClass : ExpandableObjectConverter

这里有一个非常基本的用法类:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class info : ExpandableObjectConverter
{
    private int _id;
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
}

下次别忘了那部分。。。