Click here to Skip to main content
Click here to Skip to main content

Dynamic Properties in the PropertyGrid

By , 7 Jan 2005
 

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

About the Author

Matteo Ermidoro
Web Developer
Italy Italy
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 PinmemberMazen el Senih25 Jun '12 - 4:01 
GeneralMy vote of 1 PinmemberSuperToha29 Mar '10 - 21:21 
GeneralRe: My vote of 1 PinmemberFernandaUY5 Jan '12 - 19:13 
GeneralThank you a lot PinmemberNasenbaaer4 Feb '10 - 13:14 
GeneralQuastion Pinmemberyura19894 Nov '09 - 2:27 
GeneralMy vote of 1 Pinmemberprogressma28 Aug '09 - 3:58 
GeneralRe: My vote of 1 PinmemberFernandaUY5 Jan '12 - 19:14 
QuestionSerializing/Deserializing? PinmemberDJB MASTER15 Feb '09 - 0:49 
GeneralThanks Pinmembernoi_loanxmen10 Jun '08 - 19:01 
GeneralU're the man! Pinmemberpedromonica24 Oct '07 - 5:18 
GeneralMany thanks!! PinmemberMarcelo Ricardo de Oliveira25 Jun '07 - 11:35 
GeneralThanks! PinmemberHadas_F30 May '07 - 4:44 
GeneralUpdate PropertyGrid Pinmemberberilium19 Sep '05 - 6:07 
QuestionStrange - no categories? Pinmembermathewww16 Jul '05 - 9:56 
Answernevermind ...Re: Strange - no categories? Pinmembermathewww16 Jul '05 - 10:09 
GeneralRe: nevermind ...Re: Strange - no categories? PinmemberMatteo Ermidoro17 Jul '05 - 11:06 
QuestionWhy not just use ICustomTypeDescriptor?? Pinmemberversat147417 Jan '05 - 18:01 
AnswerRe: Why not just use ICustomTypeDescriptor?? PinmemberMatteo Ermidoro18 Jan '05 - 21:13 
GeneralRe: Why not just use ICustomTypeDescriptor?? PinmemberDan Radu19 Nov '07 - 14:47 
Generalnice PinmemberNicoRi13 Jan '05 - 20:59 
GeneralRe: nice PinmemberMatteo Ermidoro14 Jan '05 - 2:58 
GeneralRe: nice PinmemberNicoRi14 Jan '05 - 4:51 
GeneralRe: nice PinmemberMatteo Ermidoro15 Jan '05 - 6:41 
GeneralRe: nice PinmemberNicoRi15 Jan '05 - 8:13 
GeneralRe: nice PinsussAnonymous16 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.

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