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

Introduction to the TypeConverter

By , 9 Mar 2004
 

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

About the Author

DanMayer
Web Developer
United States United States
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   
GeneralUse ToString()memberPeterFritzen16 Mar '04 - 20:34 
Hi,
 
If all you want to do is giving your ChildClass a nice name in the PropertyGrid, then you should consider using the ToString() method instead of creating a TypeConverter:
 
public class ChildClass
{
public string PrimaryIdentity;
public override string ToString()
{
return PrimaryIdentity;
}
}
 
This will do the trick - but does obviously not allow you to add a "ConvertFrom" method later, so the name shown will always be read-only. For many applications, this seems sufficient.
 
Cheers,

GeneralRe: Use ToString() Pinmembersahami16 Jan '07 - 6:17 
Hi
 
ToString may provide the representation you want in a propertygrid (Custom Type -> String) but how do you convert from a string to a type (String -> Custom Type)? The answer is to have a Custom TypeConverter and override the ConvertFrom - method.
See the following article for an easy sample:
http://www.codeguru.com/columns/VB/article.php/c6529/[^]
 
regards
/arash
GeneralRe: Use ToString() Pinmemberchaiguy133717 Nov '08 - 5:06 
I agree; ToString() is the simplest method to give your class a string representation in property windows and while debugging, and every class supports overriding ToString(), so no interfaces are required.
 
“It behooves every man to remember that the work of the critic, is of altogether secondary importance, and that, in the end, progress is accomplished by the man who does things.”
–Theodore Roosevelt
 
{o,o}.oO( Check out my blog! )
|)””’)          http://pihole.org/
-”-”-

GeneralRe: Use ToString() PinmemberFrank W. Wu22 Jun '09 - 13:18 
TypeConverter is typically used for design-time features. You can replace it with ToString(). It won't work.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 10 Mar 2004
Article Copyright 2004 by DanMayer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid