Dynamic Property grid problem

  • Thread starter Thread starter Ron M. Newman
  • Start date Start date
R

Ron M. Newman

Hi,

I have a problem with dynamically putting dynamic properties on the property
grid. My app has no idea what it'll be showing at compile time.

How do I dynamically add properties to the property grid?

Thanks,
Ron
 
I haven't worked with the grid a lot, just played around a few...

But from my experience, whatever object you bind it to, it will show the
properties of that object, as it is able, with respect to attributes set on
the properties and available type-converters if needed.

If you have just some set of variables, you want to show in there, I would
think you would have to wrap them in a MyVars class or something along those
lines (maybe a struct would work?). Then make an instance of your class, and
bind the grid to that instance.

HTH.
 
Hi,

Unfortunately, I am getting my list of properties and their type at runtime
and I can't "wrap" any classes there". My app can get information like: "
hey, this object now has AGE and it's an integer" at any given moment and I
need the property grid to reflect that.

Ron
 
Hi Ron,

You can create a TypeConverter for your class and override the GetProperties
and GetPropertiesSupported methods. You can add, remove or change the
properties being displayed for any Component that is defined with your
TypConverter via a TypeConverterAttribute. TypeConverterAttribute can also be
declared on properties in your class to control their behavior in the
PropertyGrid as well.

In the GetProperties method, you can use methods from the
System.ComponentModel.PropertyDescriptor class to populate a new
PropertyDescriptorCollection. In your TypeConverter you can also subclass
SimplePropertyDescriptor, a nested and protected class in the TypeConverter
class, which is an easy way to define your own simple properties.

The FCL defines many *Converter implementations that you can use as well. One
particularly interesting TypeConverter is the ExpandableObjectConverter, which
you can assign to properties of your own classes (via TypeConverterAttribute)
that are complex types and should be expandable in the PropertyGrid itself as
a +/- node.
 
Hello Ron,

Dave is right, you can use the TypeConverter to publish a set of
dynamic properties. You can also derive your class from
ICustomTypeDescriptor. I recommend you to consult the Microsoft
PropertyGrid Resource List (see signature) and search by tag to target
the right articles.

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC
Microsoft PropertyGrid Resource List -
http://www.propertygridresourcelist.com
 
If you're doing what I think your doing then you definitely need to look at
the TypeDescriptor.AddProvider method, the TypeDescriptionProvider class and
use your imagination.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top