Click here to Skip to main content
Click here to Skip to main content
Go to top

Introduction to the TypeConverter

, 9 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Illustrates simple PropertyGrid editing of class properties using TypeConverter

Introduction

There have been plenty of articles here on CodeProject discussing advanced use the PropertyGrid control. I have been asked repeatedly about these issues so I think it would help others who are trying to figure out PropertyGrid usage to start with a simplified sample of a solution to a common problem.

Problem: default view of a class property is the namespace.class

By default, a PropertyGrid will display the namespace.class string representation of an object property. Assuming the object displayed has a primary identity, it is better for the end user to see the object identity instead of the namespace.class representation. In other words, we want to change the PropertyGrid display on the left to the friendlier one on the right:

Default view With type converter

To understand how to go from one to the other, let's look at the simple sample. First, here is the code that produced the default view shown above on the left.

public class ParentClass
{
    private ChildClass _Child;
    public ChildClass Child
    {
        get
        {
            return _Child;
        }
        set
        {
            _Child=value;
        }
    }
}
public class ChildClass
{
    public string PrimaryIdentity;
}
The code shown above simply defines an object (PrimaryClass) that has an object property (ChildClass). To display the results shown in the picture on the right, we need to first create a TypeConverter for the ChildClass then declare the TypeConverter as shown in the modified code below:
public class ParentClass
{
    private ChildClass _Child;
    public ChildClass Child
    {
        get
        {
            return _Child;
        }
        set
        {
            _Child=value;
        }
    }
}
[TypeConverter(typeof(ChildClassTypeConverter))]
public class ChildClass
{
    public string PrimaryIdentity;
}
public class ChildClassTypeConverter:TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, 
        System.Globalization.CultureInfo culture, 
        object value, 
        Type destinationType)
    {
        ChildClass childClass=(ChildClass)value;
        return childClass.PrimaryIdentity;
    }        
}
The ChildClass now has a new declarative attribute: TypeConverter, which specifies the converter class which in turn inherits TypeConverter. The new converter class specifies a single override: ConvertTo. The ConvertTo method simply converts a ChildClass into a string. As shown, we simply return the objects' default primary identity property.

Note that to make this editable, simply create an override for the ConvertFrom method. I have not done this because doing so would require a call to a database to return the object specified by the primary key and I want to keep this illustration as simple and easy to understand as possible.

The System.ComponentModel namespaces specifies 27 type converter classes ranging from ArrayConverter to UInt64Converter. Here you can see how you can create your own type converter classes. Simply inherit TypeConverter (or one of the other 27 base TypeConverter types) and implement the overrides as neccessary. Inherit ExpandableObjectConverter if your object property should be displayed on it's own node level. For example, you might have an object 'Address' that has Street, City State and PostalCode properties.

When the TypeConverter is inadaquate and you must provide your own interface, you simply create a class that derives from UITypeEditor and overrides EditValue with some code that shows a form applicable to your content.

Now that you've seen how simple it is to get user-friendly views of your object properties in the property grid, you'll want to go on to the next steps that are covered in more detail in other articles on this site.

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

DanMayer
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralUI Type Editor Questions PinmemberMember 223871423-Apr-08 16:55 
GeneralThanks Pinmemberosirisgothra25-Aug-07 18:10 
GeneralUse ToString() PinmemberPeterFritzen16-Mar-04 20:34 
GeneralRe: Use ToString() Pinmembersahami16-Jan-07 6:17 
GeneralRe: Use ToString() Pinmemberchaiguy133717-Nov-08 5:06 
GeneralRe: Use ToString() PinmemberFrank W. Wu22-Jun-09 13:18 

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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 10 Mar 2004
Article Copyright 2004 by DanMayer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid