Click here to Skip to main content
11,641,971 members (67,960 online)
Click here to Skip to main content

Dynamic Properties in the PropertyGrid

, 7 Jan 2005 132.7K 2.1K 67
Rate this:
Please Sign up or sign in to vote.
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.
interface ICustomClass
{
    PropertyList PublicProperties
    {
        get;
        set;
    }
}

Adding a new property in the Propertygrid is simple:

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.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:

//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.

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.

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:

new PropertyDescriptorCollection(arrProp);

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

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

Share

About the Author

Matteo Ermidoro
Web Developer
Italy Italy
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mazen el Senih25-Jun-12 4:01
memberMazen el Senih25-Jun-12 4:01 
GeneralMy vote of 1 Pin
SuperToha29-Mar-10 21:21
memberSuperToha29-Mar-10 21:21 
GeneralRe: My vote of 1 Pin
FernandaUY5-Jan-12 19:13
memberFernandaUY5-Jan-12 19:13 
GeneralThank you a lot Pin
Nasenbaaer4-Feb-10 13:14
memberNasenbaaer4-Feb-10 13:14 
GeneralQuastion Pin
yura19894-Nov-09 2:27
memberyura19894-Nov-09 2:27 
GeneralMy vote of 1 Pin
progressma28-Aug-09 3:58
memberprogressma28-Aug-09 3:58 
GeneralRe: My vote of 1 Pin
FernandaUY5-Jan-12 19:14
memberFernandaUY5-Jan-12 19:14 
QuestionSerializing/Deserializing? Pin
DJB MASTER15-Feb-09 0:49
memberDJB MASTER15-Feb-09 0:49 
GeneralThanks Pin
noi_loanxmen10-Jun-08 19:01
membernoi_loanxmen10-Jun-08 19:01 
GeneralU're the man! Pin
pedromonica24-Oct-07 5:18
memberpedromonica24-Oct-07 5:18 
GeneralMany thanks!! Pin
Marcelo Ricardo de Oliveira25-Jun-07 11:35
memberMarcelo Ricardo de Oliveira25-Jun-07 11:35 
GeneralThanks! Pin
Hadas_F30-May-07 4:44
memberHadas_F30-May-07 4:44 
GeneralUpdate PropertyGrid Pin
berilium19-Sep-05 6:07
memberberilium19-Sep-05 6:07 
QuestionStrange - no categories? Pin
mathewww16-Jul-05 9:56
membermathewww16-Jul-05 9:56 
Answernevermind ...Re: Strange - no categories? Pin
mathewww16-Jul-05 10:09
membermathewww16-Jul-05 10:09 
GeneralRe: nevermind ...Re: Strange - no categories? Pin
Matteo Ermidoro17-Jul-05 11:06
memberMatteo Ermidoro17-Jul-05 11:06 
QuestionWhy not just use ICustomTypeDescriptor?? Pin
versat147417-Jan-05 18:01
memberversat147417-Jan-05 18:01 
AnswerRe: Why not just use ICustomTypeDescriptor?? Pin
Matteo Ermidoro18-Jan-05 21:13
memberMatteo Ermidoro18-Jan-05 21:13 
GeneralRe: Why not just use ICustomTypeDescriptor?? Pin
Dan Radu19-Nov-07 14:47
memberDan Radu19-Nov-07 14:47 
Generalnice Pin
NicoRi13-Jan-05 20:59
memberNicoRi13-Jan-05 20:59 
GeneralRe: nice Pin
Matteo Ermidoro14-Jan-05 2:58
memberMatteo Ermidoro14-Jan-05 2:58 
GeneralRe: nice Pin
NicoRi14-Jan-05 4:51
memberNicoRi14-Jan-05 4:51 
GeneralRe: nice Pin
Matteo Ermidoro15-Jan-05 6:41
memberMatteo Ermidoro15-Jan-05 6:41 
GeneralRe: nice Pin
NicoRi15-Jan-05 8:13
memberNicoRi15-Jan-05 8:13 
GeneralRe: nice Pin
Anonymous16-Jan-05 22:41
sussAnonymous16-Jan-05 22:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 7 Jan 2005
Article Copyright 2005 by Matteo Ermidoro
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid