Custom ITypeDescriptorContext

J

jonpb

Hi,
I need to have context information in order to convert a string to my
object so that I can use the PropertyGrid control.
TypeConverter.ConvertFrom takes an ITypeDescriptorContext, but I cannot
figure out how to have a custom ITypeDescriptorContext passed to my
TypeConverter implementation from the PropertyGrid. Is this possible, or
am I barking up the wrong tree?

Basically, I have a simple FixedLengthString object which has a string
value and a capacity. In order to convert from a string the
TypeConverter needs to know what the capacity of the source object is.

Thanks
 
M

Marc Gravell

The context is passed from the caller, in this case the PropertyGrid -
but you shouldn't need to have a custom context unless you are writing
your own UI implementation. Normally, the trick is to look at the
context's Instance and PropertyDescriptor (if they are set) which gives
you the object and property being processed. A typical use-case would be
to look at the PropertyDescriptor for metadata (attributes etc).

If you let me know what you need to do in more detail I might be able to
help more (I'm painfully familiar with System.ComponentModel).

Note also that some simpler data-binding implementations (like
DataGridView) pass a null context; you should be fine from PropertyGrid,
though.

Marc
 
J

jonpb

Marc said:
The context is passed from the caller, in this case the PropertyGrid -
but you shouldn't need to have a custom context unless you are writing
your own UI implementation. Normally, the trick is to look at the
context's Instance and PropertyDescriptor (if they are set) which gives
you the object and property being processed. A typical use-case would be
to look at the PropertyDescriptor for metadata (attributes etc).

If you let me know what you need to do in more detail I might be able to
help more (I'm painfully familiar with System.ComponentModel).

Note also that some simpler data-binding implementations (like
DataGridView) pass a null context; you should be fine from PropertyGrid,
though.

Marc

Thanks for your help Marc, I got it now:

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo info, object value)
{
if (value is string)
{
if (context != null)
{
PropertyDescriptor pd = context.PropertyDescriptor;
FixedLengthString fs =
(FixedLengthString)context.Instance.GetType().GetProperty(pd.Name).GetValue(context.Instance,
null);
if (fs != null)
return new FixedLengthString((string)value, fs.Capacity);
}
}
return base.ConvertFrom(context, info, value);
}
 
M

Marc Gravell

Re the "get" code... how about:

FixedLengthString fs = (FixedLengthString)
pd.GetValue(context.Instance);

PropertyDescriptor is an abstract "property" implementation; by
default it uses reflection under the bonnet, so you don't need to use
reflection yourself. This approach also means that your code will work
with more exotic property systems [of which many are possible...].

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top