By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Re: Custom ITypeDescriptorContext

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.