471,348 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Re: Custom ITypeDescriptorContext

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
Jul 14 '08 #1
2 5165
Marc Gravell wrote:
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().GetP roperty(pd.Name).GetValue(context.Instance,
null);
if (fs != null)
return new FixedLengthString((string)value, fs.Capacity);
}
}
return base.ConvertFrom(context, info, value);
}

Jul 14 '08 #2
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
Jul 15 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tom | last post: by
5 posts views Thread by ToddH | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.