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

目前,我有一个由PropertyGrid查看的A型对象.然而,其属性之一是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; }
    }
}

解决方法

为此可以使用 ExpandableObjectConverter类.

This class adds support for properties
on an object to the methods and
properties provided by TypeConverter.
To make a type of property expandable
in the PropertyGrid,specify this
TypeConverter for standard
implementations of
GetPropertiesSupported and
GetProperties.

要使用此转换器,请使用TypeConverterAttribute装载有问题的属性,使用typeof(ExpandableObjectConverter)作为构造函数.

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

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public B BooFar { get; set; }
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...