Napoleon
Napoleon

Reputation: 1102

PropertyGrid display class members that are also classes

I have the class PGMain as the SelectedObject in the propertygrid:

         [DefaultPropertyAttribute("Basic")]
[Serializable]
public class PGMain
{    
    private TestClass m_TEST = new TestClass();
    [CategoryAttribute("TEST")]
    public TestClass TEST
    {
        get { return m_TEST; }
        set { m_TEST = value; }
}
    // More members are here
}

Now I would like to expand the members of the TestClass in the PropertyGrid. So I tried the following:

   [Serializable]
[DescriptionAttribute("Expand to see the options for the application.")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class TestClass : ExpandableObjectConverter
{
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]        
    public string Name = "";
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
    public object Value = null;
    [CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
    public bool Include = true;

    public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
    {
        if (destinationType == typeof(TestClass))
            return true;

        return base.CanConvertTo(context, destinationType);
    }
}

The result is that there is an expandable-icon in front of the TestClass in the propertygrid but it can not be expanded. What am I missing? Just to be clear: I can show expandable members of the PGMain class but NOT expandable members of the members of the PGMain class like the Test-member in PGMain.


Edit:

No I have 2 classes NOT 1.

  [DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooA
{
    private fooB m_TestMember = new fooB();
    [Browsable(true)]
    [CategoryAttribute("Test category"), DescriptionAttribute("desctiption here")] // <<<<< this one works.
    [TypeConverter(typeof(fooB))]
    public fooB TestMember
    {
        get { return m_TestMember; }
        set { m_TestMember = value; }
    }

}

[DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooB
{
    private string m_ShowThisMemberInGrid = "it works"; // <<<<< this doesn NOT work
    [CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
    public string ShowThisMemberInGrid
    {
        get { return m_ShowThisMemberInGrid; }
        set { m_ShowThisMemberInGrid = value; }
    }

    public override string ToString()
    {
        return "foo B";
    }
}

But I did solve the problem (by coincidence). It appears that public variables are not listed in the propertygrid. It HAVE to be properties with getters and setters. That was the solution. So the above snippet solved the problem. Thanks for your replies anyway :).

Upvotes: 1

Views: 11438

Answers (2)

Napoleon
Napoleon

Reputation: 1102

Wrong:

[CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
public string Name = "";

Good:

    private string m_Name = new string();
    [CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; }
    }

Upvotes: 4

Anuraj
Anuraj

Reputation: 19598

Sorry I misinterpret the question.

You can find more details on these links

  1. http://msdn.microsoft.com/en-us/library/aa302326.aspx#usingpropgrid_topic6a
  2. http://www.codeproject.com/KB/miscctrl/bending_property.aspx
  3. http://msdn.microsoft.com/en-us/library/aa302334.aspx

Hope it helps :)

UPDATE:

I copied the code from here

And modified like this.

public class SamplePerson
{
    public string Name
    {
        get;
        set;
    }
    public Person Person
    {
        get;
        set;
    }
}

And in the form I have done something like

SamplePerson h = new SamplePerson();
h.Person = new Person
{
    Age = 20,
    FirstName = "f",
    LastName = "l"
};
this.propertyGrid1.SelectedObject = h;

And its working for me.

Property Grid

Provide Browsable as false for properties you don't want to display in the property grid.

[Browsable(false)]
public bool Include
{
get;set;
}

Upvotes: 1

Related Questions