简体   繁体   English

如何在PropertyGrid中显示带有子类的Object

[英]How to display Object with sub-class in PropertyGrid

I'm going to use PropertyGrid to show my Objects. 我将使用PropertyGrid来显示我的对象。 Here is the info class. 这是info类。 The Info class has some properties which is composed of the class type. Info类有一些由类类型组成的属性。 However, the sub-class doesn't show the properties. 但是,子类不显示属性。 Do you have any idea? 你有什么主意吗?

Code snippet: 代码段:

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

Edited However, struct case doesn't effect for [TypeConverter(typeof(ExpandableObjectConverter))] attribute. 编辑但是,struct case对[TypeConverter(typeof(ExpandableObjectConverter))]属性没有影响。 Do you have any idea ? 你有什么主意吗 ?

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

public struct DoublePoint
{
        public double x, y;
}

You need to use TypeConverter: 你需要使用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; }
    }

Your code is fine, but you just forgot a tiny little extra info in your class declaration: public class MyClass : ExpandableObjectConverter . 你的代码很好,但你只是忘了你的类声明中的一小部分额外信息: public class MyClass : ExpandableObjectConverter

Here's a very basic usage class: 这是一个非常基本的用法类:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class info : ExpandableObjectConverter
{
    private int _id;

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

}

Don't forget that part next time... 下次不要忘记那部分......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM