Dynamic PropertyGrid properties

sab669 picture sab669 · May 21, 2012 · Viewed 19.1k times · Source

So for this project I'm working on, we've decided to use the .NET PropertyGrid control. The propertygrid gets populated with an object that is built during run time, based off of what item is selected by the user in a ListView control.

So, if they select the first item in the ListView such as "Base", the PropertyGrid will show the properties for that component, such as its dimensions. Then they select "Top" and it will show color in the PropertyGrid. Either way, every item in the list is one single "component" object.

Basically, when a ListView item is selected, a loop iterates through a data set to find what properties are associated with that selected component object, then gets thrown into the propertybag class which is displayed in the propertygrid.

What I'm trying to figure out, since these components and properties are all one class, how can I dynamically determine which properties should be a displayed as a drop down menu, or image box, or a text-field.

I'm using Visual Studios 2010 / C#.NET, and for the dynamic property generation I'm using the apparently-popular "Property Bag" class I found on CodeProject by Tony Allowatt. The only thing I can think of would be to maybe add an extra column to the database for the properties and use that to tell the PropertyBag what data type to add? It seems to be a popular topic, but I'm having difficulties figuring out how to do it in conjunction with a dynamically built object.

Answer

Keith Vinson picture Keith Vinson · May 26, 2012

Its not an answer per se, but I too have been working up to building such a beast. Here is stackoverflow's greatest hits on the topic...

How to modify PropertyGrid at runtime (add/remove property and dynamic types/enums)

How to display a dynamic object in property grid?

PropertyGrid and Dynamic Types of Objects

At first I though I actually needed dynamic objects based on the Expando object, for me it turns out not to be the case. You might want to make sure you to don't fall into that trap.

In my case what I really needed was a collection of custom objects that can have a variable set of properties added to them. Where each property is an instantiation of one of three custom types (stringType, rangeType, or enumType). Once I realized the "dynamic" properties were not going to be of arbitrary class types the project became an easy twist on the code discussed in the three stackoverflow examples. With the question How to modify PropertyGrid at runtime (add/remove property and dynamic types/enums) being almost a direct example of what I am ending up with.

Hope my ramblings help you find your path...