Click here to Skip to main content
13,351,044 members (68,324 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 4 Jan 2006

Automatic Expandable Properties in a PropertyGrid

, 4 Jan 2006
Rate this:
Please Sign up or sign in to vote.
The article describes a family of classes that will automatically enable each of your custom-type's public properties to be expandable in a PropertyGrid without the need to write an explicit TypeConverter. You will also be able to edit those properties that support the 'set' accessor.


This article explains adding PropertyGrid support to your custom-types without an associated TypeConverter.

When I finally bit the bullet and moved from Visual Studio 2002 up to Visual Studio 2005, I was disappointed (to put it mildly) to discover that when I selected an instance of my Form-class into a PropertyGrid, none of my custom-type public properties were either expandable or editable as (I am sure!) they used to be. All I could see for each of my properties was a single, relatively useless GridItem in grey-text showing <namespace>.<type-name>.

Of course, by overriding ToString() in each of my custom-types, I could have made the display a little more aesthetically pleasing, I could even display the type’s fields and properties using this approach. However, this wouldn’t have allowed any of those objects to be edited, or even indicate which one’s were editable and which were read-only. Surely, the most useful feature of PropertyGrid a'la 2002 was the way it allowed you to very rapidly provide a type-editor/inspector for your custom-types.

Initially, I spent a day, or so, decorating my classes and their members with various combinations of ComponentModel attribute-tags, in an attempt to get the behaviour that:

  1. I wanted, and
  2. the attribute-names suggested they would provide.

Hah! There followed then a very painful 3-4 days scouring MSDN, forums, etc., initially trying to discover Microsoft’s explanation for the change in behaviour and an associated work-around (fat chance!); then, when this search proved fruitless, to find someone else who had hit and, more importantly solved the same problem. I didn’t have much luck. I did find a few posts on various sites from one Dev. Manager in the US who had been through the same thing back in June this year, but when I mailed him to ask about a solution, he wrote back and said he’d given-up and gone another route.

Finally, I began to realize, although I admit, it took some time for me to accept it, that the only solution (according to Microsoft documentation at least) was to write a custom TypeConverter for every one of my custom-property types! This would enable PropertyGrid to see each custom-type as the aggregation of subtypes that I wanted. This seemed like a pretty big ask!

Then I found Stephen Toub’s excellent NetMatters articles: see URLs below in Background.

  • NET Matters ICustomTypeDescriptor, Part 1 -- MSDN Magazine, April 2005
  • NET Matters ICustomTypeDescriptor, Part 2 -- MSDN Magazine, May 2005

…in which he introduces a family of classes: FieldsToPropertiesTypeDescriptor, FieldsToPropertiesTypeDescriptorProvider, etc., whose purpose is, given an existing type MyClass (say) that exposes only public-access fields (i.e. no Property support), to automatically wrap each field in a pseudo-property that will display in a PropertyGrid without your having to modify the original MyClass at all.

I would encourage anyone interested in PropertyGrid to read these two articles to gain a better understanding of its internal operation; also, to help understand the code presented here.

Although FieldsToPropertiesTypeDescriptor, et. al. didn’t quite provide the desired behaviour, it was possible to use the same technique and architecture to create a solution to my own particular problem. The results are presented below as ExpandableObject and ExpandablePropertiesTypeDescriptionProvider.

Note that the cache-mechanism used in ExpandableObject, in addition to the basic architecture, is lifted from the code in Stephen Taub’s article. Thanks to the author for the information/techniques covered therein and permission to reuse for non-profit making purposes.


Recommended reading:

Using the code

To add support for PropertyGrid expandable/editable public properties to your class, simply derive it from ExpandableObject, e.g.:

using ExpandablePropertiesTypeDescriptor;

public class MyCustomType : ExpandableObject
    private int myIntField = 0;
    public int MyIntProperty
        get{ . . . }
        set{ . . . }

Now, when you select an instance of MyCustomType into a PropertyGrid, e.g.:

propertyGrid1.SelectedObject = MyCustomTypeInstance;

you will see all its public-access properties displayed under the usual [+] widget. Any property that supports ‘set’ will also be editable in the PropertyGrid.

Note that, if it is not convenient for you to inherit from ExpandableObject, e.g., the object that you want to select in PropertyGrid is a Form (say) and you want the PropertyGrid to display its (the Form’s) public properties, you can do the following:

using ExpandablePropertiesTypeDescriptor;

public partial class MyFormClass : Form
    public MyFormClass()


If you use the scenario shown above, be sure to call TypeDescriptor.RemoveProvider before your application closes. You can do this in the Form's Dispose() method as follows:

protected override void Dispose(bool disposing)
    if (disposing && (components != null))

Points of Interest

Check-out the ExpandableObjectDemo.DemoForm to see the ExpandableObject used on a custom-type property owned by a form. Notice that both techniques [described above] are used, i.e., ExpandableObjectDemo.CustomType inherits from ExpandableObject whilst ExpandableObjectDemo.DemoForm calls TypeDescriptor.AddProvider / TypeDescriptor.RemoveProvider. This is because it is the Form object that is selected in PropertyGrid. If we only wanted to display ExpandableObjectDemo.CustomType in the PropertyGrid without showing its relationship to the parent object, we need not call the Add/RemoveProvider in the Form object.

I guess the most important lesson I have (re)learned during this episode was: “Don’t mess with your dev. environment unless you (i) enjoy pain and (ii) have nothing better [or more urgent] to do.


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

United Kingdom United Kingdom
The whole world's a circus... don't you be the clown

You may also be interested in...

Comments and Discussions

QuestionGood job, but ... Pin
DominiqueRT28-Oct-11 3:26
memberDominiqueRT28-Oct-11 3:26 
AnswerRe: Good job, but ... Pin
DominiqueRT1-Nov-11 1:00
memberDominiqueRT1-Nov-11 1:00 
I answers to me, here is a solution:

public override TypeConverter Converter ...

if (base.Converter.GetType() != typeof(TypeConverter))

and replace by
if (base.Converter.GetType() != typeof(TypeConverter) && base.Converter.GetType() != typeof(ReferenceConverter))

QuestionGreat article Pin
Kelvin3324-Oct-11 19:59
memberKelvin3324-Oct-11 19:59 
QuestionReinventing the wheel? Pin
Reza_the_wager12-Mar-07 23:58
memberReza_the_wager12-Mar-07 23:58 
QuestionDo you have a C++/CLI version of this? Pin
dlk573026-Jan-07 12:20
memberdlk573026-Jan-07 12:20 
AnswerRe: Do you have a C++/CLI version of this? Pin
dlk573028-Jan-07 5:16
memberdlk573028-Jan-07 5:16 
GeneralNice job! Pin
Greg Cadmes4-Aug-06 7:14
memberGreg Cadmes4-Aug-06 7:14 
GeneralAnother way(?) Pin
joaeri1-Feb-06 0:27
memberjoaeri1-Feb-06 0:27 
GeneralRe: Another way(?) Pin
rahtrow25-Feb-06 5:58
memberrahtrow25-Feb-06 5:58 
GeneralRe: Another way(?) Pin
Nice Life19-Apr-06 22:32
memberNice Life19-Apr-06 22:32 
GeneralRe: Another way(?) Pin
GreatPumpkinator30-Sep-06 17:03
memberGreatPumpkinator30-Sep-06 17:03 
GeneralLiked article, need source! Pin
Peter Todd24-Jan-06 10:51
memberPeter Todd24-Jan-06 10:51 
AnswerRe: Liked article, need source! Pin
RichAhem31-Jan-06 9:36
memberRichAhem31-Jan-06 9:36 
GeneralWhy has it changed from 2003 to 2005 Pin
jonx10-Jan-06 11:52
memberjonx10-Jan-06 11:52 
GeneralRe: Why has it changed from 2003 to 2005 Pin
RichAhem31-Jan-06 9:43
memberRichAhem31-Jan-06 9:43 
GeneralNo ExpandableObject Pin
Tonster1014-Jan-06 13:55
memberTonster1014-Jan-06 13:55 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 4 Jan 2006
Article Copyright 2006 by RichAhem
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid