Click here to Skip to main content
15,887,476 members
Articles / Programming Languages / C#
Article

Dynamic Properties in the PropertyGrid

Rate me:
Please Sign up or sign in to vote.
4.51/5 (24 votes)
7 Jan 20052 min read 177.6K   2.9K   70   28
How to modify the properties shown in a propertygrid at runtime.

Sample Image - DynPropGrid.jpg

Introduction

The article is mainly to explain how to make PropertyGrid show only some properties of a class. One of the big limitations of this control is that it's not possible to hide or show any class properties at runtime.

For example, in an application that I'm developing, I had to show in the PropertyGrid a class that inherits from XMLElement, but I didn't want all the properties of the XMLElement to be shown to the user. One solution was to create a new class that makes access to the main class, but this solution was too less expandable and not useful at runtime. So, I created this control that lets me control the properties shown in the PropertyGrid at runtime too.

Using the code

The class to be shown in the PropertyGrid must inherit from ICustomClass which has only one property, PublicProperties. This is a collection of myPropertys. myProperty implements two properties:

  • Name: the name of the property to be shown.
  • Category: the name of the category to be shown in the Propertygrid.
C#
interface ICustomClass
{
    PropertyList PublicProperties
    {
        get;
        set;
    }
}

Adding a new property in the Propertygrid is simple:

C#
c.PublicProperties.Add(new myProperty("test3","Category1"));

In this line, we want to show in the PropertyGrid the property "test3" in the category "Category1".

Now, to remove the property from the control:

C#
c.PublicProperties.Remove("test3");

Now, let's check the PropertyGrid. The properties are visualized in the PropertyTab object. So it's necessary to create a new PropertyTab to access the visualized rows. To filter out the properties visualized, I've to override the getProperties method of the PropertyTab class.

This method has an input parameter, the object to be shown in the PropertyGrid. And this object must implement the ICustomClass interface so that I can get the properties I want to display in the PropertyGrid within the GetProperties method.

To get all the properties of the selected object, there's the TypeDescriptor.GetProperties(component). This function returns a PropertyDescriptorCollection filled with all the properties of the class to be shown. But, as I said before, the class to be shown in the PropertyGrid must implement ICustomClass.

So with this row:

C#
//Componet must implement the ICUSTOMCLASS interface.
ICustomClass bclass=(ICustomClass)component;

it's possible to retrieve the list of properties to be shown in the PropertyTab. It's necessary to create a new PropertyDescriptorfor for each new property.

C#
PropertyDescriptor[] propArray = 
        new PropertyDescriptor[bclass.PublicProperties.Count];

Now I've to create the new property that will be shown in the PropertyGrid. So I used TypeDescriptor.CreateProperty from System.ComponentModel.

C#
for (int i=0;i<bclass.PublicProperties.Count;i++)
{
     //Find the list of properties in the array of properties 
     //whose name is in the PubliCProperties
     PropertyDescriptor prop=
             properties.Find(bclass.PublicProperties[i].Name,true);    
     //Build a new properties
     arrProp[i] = TypeDescriptor.CreateProperty(prop.ComponentType, prop, 
            new CategoryAttribute(bclass.PublicProperties[i].Category));
}

At the end of the loop, the arrProp array will be filled up with the new property to be shown in the PropertyTab. Now, since Getproperties has a PropertyDescriptorCollection as return parameter, we can create a new instance of it with:

C#
new PropertyDescriptorCollection(arrProp);

The last step to get everything working, is to override the creation of the PropertyTab of the PropertyGrid.

C#
protected override PropertyTab CreatePropertyTab(Type tabType)

In this function, the base creation of the Propertytab is bypassed, and only the filtered Propertytab is created.

History

  • Original article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mazen el Senih25-Jun-12 4:01
professionalMazen el Senih25-Jun-12 4:01 
GeneralMy vote of 1 Pin
SuperToha29-Mar-10 21:21
SuperToha29-Mar-10 21:21 
GeneralRe: My vote of 1 Pin
FernandoUY5-Jan-12 19:13
professionalFernandoUY5-Jan-12 19:13 
GeneralThank you a lot Pin
Nasenbaaer4-Feb-10 13:14
Nasenbaaer4-Feb-10 13:14 
GeneralQuastion Pin
yura19894-Nov-09 2:27
yura19894-Nov-09 2:27 
GeneralMy vote of 1 Pin
progressma28-Aug-09 3:58
progressma28-Aug-09 3:58 
GeneralRe: My vote of 1 Pin
FernandoUY5-Jan-12 19:14
professionalFernandoUY5-Jan-12 19:14 
QuestionSerializing/Deserializing? Pin
DJB MASTER15-Feb-09 0:49
DJB MASTER15-Feb-09 0:49 
GeneralThanks Pin
noi_loanxmen10-Jun-08 19:01
noi_loanxmen10-Jun-08 19:01 
GeneralU're the man! Pin
pedromonica24-Oct-07 5:18
pedromonica24-Oct-07 5:18 
GeneralMany thanks!! Pin
Marcelo Ricardo de Oliveira25-Jun-07 11:35
Marcelo Ricardo de Oliveira25-Jun-07 11:35 
GeneralThanks! Pin
Hadas_F30-May-07 4:44
Hadas_F30-May-07 4:44 
GeneralUpdate PropertyGrid Pin
berilium19-Sep-05 6:07
berilium19-Sep-05 6:07 
QuestionStrange - no categories? Pin
mathewww16-Jul-05 9:56
mathewww16-Jul-05 9:56 
Answernevermind ...Re: Strange - no categories? Pin
mathewww16-Jul-05 10:09
mathewww16-Jul-05 10:09 
GeneralRe: nevermind ...Re: Strange - no categories? Pin
Matteo Ermidoro17-Jul-05 11:06
Matteo Ermidoro17-Jul-05 11:06 
QuestionWhy not just use ICustomTypeDescriptor?? Pin
Mike Lang17-Jan-05 18:01
Mike Lang17-Jan-05 18:01 
AnswerRe: Why not just use ICustomTypeDescriptor?? Pin
Matteo Ermidoro18-Jan-05 21:13
Matteo Ermidoro18-Jan-05 21:13 
GeneralRe: Why not just use ICustomTypeDescriptor?? Pin
Dan Radu19-Nov-07 14:47
Dan Radu19-Nov-07 14:47 
Generalnice Pin
NicoRi13-Jan-05 20:59
NicoRi13-Jan-05 20:59 
GeneralRe: nice Pin
Matteo Ermidoro14-Jan-05 2:58
Matteo Ermidoro14-Jan-05 2:58 
GeneralRe: nice Pin
NicoRi14-Jan-05 4:51
NicoRi14-Jan-05 4:51 
There is another way of using dynamic properties: you can create a type at
runtime that accesses certain properties of your original object with its
properties. Create an instance of it and let the PropertyGrid select it instead of the original object. I implemented this solution using MSIL. But I guess your method is faster and easier to handle. With my method as soon as you want to remove or add properties at runtime the dynamic object has to be completely recreated. But an advantage is that you are even more flexible: you can for instance add properties that access other objects properties or interfaces that access interfaces in other objects.

example:

DynamicObjectContainer
{
public void addProperty(Object propertyOwner, PropertyInfo property);
public void createDynamicObject();
public void deleteDynamicObject();
public Object DynamicObject
{ get; }
}

So you could write a class looking like this. With addProperty you add
any property from any object. With createDynamicObject you create the dynamic object after you added all properties you wanted to add.
And then you let your PropertyGrid select the DynamicObject.

In my implementation that works fine but it could be a little slow, maybe.

Nico
GeneralRe: nice Pin
Matteo Ermidoro15-Jan-05 6:41
Matteo Ermidoro15-Jan-05 6:41 
GeneralRe: nice Pin
NicoRi15-Jan-05 8:13
NicoRi15-Jan-05 8:13 
GeneralRe: nice Pin
Anonymous16-Jan-05 22:41
Anonymous16-Jan-05 22:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.