如何在PropertyGrid中查看对象属性?

Min*_*wth 11 c# propertygrid expand view

目前,我有一个类型为A的对象,正由PropertyGrid查看.但是,它的一个属性是B类.B类属性是不可扩展的.我怎样才能改变这一点:

a)我可以展开自定义对象属性b)这些更改绑定到该属性

这是我到目前为止的代码:

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            A a = new A
            {
                Foo = "WOO HOO!",
                Bar = 10,
                BooFar = new B
                {
                    FooBar = "HOO WOO!",
                    BarFoo = 100
                }
            };

            propertyGrid1.SelectedObject = a;
        }
    }
    public class A
    {
        public string Foo { get; set; }
        public int Bar { get; set; }
        public B BooFar { get; set; }
    }
    public class B
    {
        public string FooBar { get; set; }
        public int BarFoo { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*Ani 22

您可以将该ExpandableObjectConverter类用于此目的.

此类将对象的属性支持添加到TypeConverter提供的方法和属性.要使一种属性在PropertyGrid中可扩展,请为GetPropertiesSupported和GetProperties的标准实现指定此TypeConverter.

要使用此转换器,请使用TypeConverterAttributewith typeof(ExpandableObjectConverter)作为constructor-argument来装饰相关属性.

public class A
{
    public string Foo { get; set; }
    public int Bar { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public B BooFar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)