PropertyGrid:ExpandableObjectConverter出现问题

时间:2012-03-12 15:39:35

标签: c# propertygrid

我创建了一个要在PropertyGrid中显示的类,其中包含另一个类;我希望这个类可以扩展,所以我尝试添加[TypeConverter(typeof(ExpandableObjectConverter))]但它似乎不起作用。这是我试过的一个简单例子:

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

        this.propertyGrid1.SelectedObject = new Class1();
    }
}

public class Class1
{
    string name;

    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    Class2 class2;

    public Class2 Class2
    {
        get { return this.class2; }
        set { this.class2 = value; }
    }
}

[TypeConverter(typeof(ExpandableObjectConverter))]
public class Class2
{
    string stuff = "none";

    public string Stuff
    {
        get { return this.stuff; }
        set { this.stuff = value; }
    }
}

当在属性网格中显示时,Class2实例的Class1属性不可展开。为什么这不起作用的任何想法?

谢谢!

1 个答案:

答案 0 :(得分:7)

Class2类型的属性未展开,因为它为null。只是实例化你的财产,一切都会好的:

Class2 class2 = new Class2();
相关问题