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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.