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

Introduction to the TypeConverter

Rate me:
Please Sign up or sign in to vote.
3.66/5 (20 votes)
9 Mar 20042 min read 113.7K   849   31   6
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 viewWith 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.

C#
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:
C#
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


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

Comments and Discussions

 
GeneralUI Type Editor Questions Pin
Member 223871423-Apr-08 16:55
Member 223871423-Apr-08 16: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.