[.net] PropertyGrid - Nested Elements

Started by
4 comments, last by Mike.Popoloski 16 years, 8 months ago
I'm pointing the PropertyGrid control at an object which has a number of properties. If the properties are of a type that has its own properties I'd like those properties to be visible too. If the property is of an existing type, such as a Button, then all the subproperties are displayed by expanding the item with its [+] sign. However, my custom types just appear with their .ToString() name, and aren't expandable to see the individual properties within. What does one need to do to make their custom times "expandable" when nested as properties in other types and viewed in a PropertyGrid?

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Advertisement
I am not quite sure whether you know about the custom type descriptor system, in case you don't there is this link here. I think the general idea is that you nest the type descriptions and hand it to the grid.

Hope that somehow helps,

Dave
I'm sorry, I'm not sure what I should be looking for. [sad]

public class SomeClassWithProperties {    private string name;    public string Name {        get { return this.name; }    }    public override ToString() {        return "This doesn't work.";    }}public class TestClass {    private Button buttonProperty;    public Button ButtonProperty {        get { return this.buttonProperty; }    }    private SomeClassWithProperties someProperty;    public SomeClassWithProperties SomeProperty {        get { return this.someProperty; }    }}
Let's say you created an instance of TestClass and viewed it in the PropertyGrid. It would list two properties. You could expand the ButtonProperty and see the properties exposed by Button fine, but you couldn't do anything with SomeProperty - it just displays "This doesn't work." and I'd like it to expand with a [+] and show the Name property, like it does for the Button's properties.

Thanks for the advice, though. [smile]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

You'd most likely be looking for the ExpandableObjectConverter

http://msdn2.microsoft.com/en-us/library/system.componentmodel.expandableobjectconverter.aspx

Alright a example

class PropertyGridClass{  private ExpandableClass m_class;  public ExpandableClass Class {     get { return m_class; }    set { m_class = value; }  }}[TypeConverter(typeof(ExpandableObjectConverter))]public class ExpandableClass{  private string m_name;  public string Name  {     get { returm m_name; }  } } 
Thanks, looks like it'll do the trick. [smile]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Take a look at the DesignerSerializationVisibility attribute, specifically the Content value for the enumeration.

Actually, come to think of it, that only helps Visual Studio serialize your code properly. In order to get the expandability (word?), you need to add an ExpandableObjectConverter type converter to your class, like so:
[TypeConverter(typeof(ExpandableObjectConverter)]public class Foo{}

That should do the trick.

I am not entirely sure that that is the right attribute you are looking for. There are so many that interact with the Visual Studio designer that I get confused so much. I am at work right now, but if that doesn't work and you can wait until I get home later, I can find out exactly using a great book I have that covers everything there is to know about providing design time support.

EDIT: Oops! Waaaay too slow.
Mike Popoloski | Journal | SlimDX

This topic is closed to new replies.

Advertisement