4

I have created a class to display in a PropertyGrid which contains another class; I would like this class to be expandable so I tried adding the [TypeConverter(typeof(ExpandableObjectConverter))] but it doesn't seem to work. Here is a simple example I tried:

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

When displayed in the property grid, the Class2 property of the Class1 instance isn't expandable. Any idea on why this isn't working?

Thanks!

john
  • 4,043
  • 7
  • 27
  • 39

1 Answers1

7

Your property of type Class2 is not expanding because it is null. Just instantiate your property and all will be fine:

Class2 class2 = new Class2();
Dmitriy Konovalov
  • 1,777
  • 14
  • 14