Propertygrid disable item at runtime

G

Guest

Hi there,

is it possible to disable a property in the propertygrid at runtime?
Normally a property will be enabled but if i.e. the user doesn't have access
to change the property this property has to be disabled.

Is this possible?

Thx in advance.
 
M

Marc Gravell

You can do this if you provide your own view on the properties. For
sub-properties, the primary list is provided by the TypeConverter, but
often (e.g. ExpandableObjectConverter) this uses values from
TypeDescriptor.GetProperties.

What you would need to do is to return some custom PropertyDescriptor
instances, usually just as a wrapper around your existing (reflective)
PropertyDescriptor, but override IsReadOnly.

If using the TypeConverter approach, then simply override
GetProperties(); if you also want TypeDescriptor to behave the same,
then in 1.1 implement ICustomTypeDescriptor, or in 2.0 consider
providing a TypeDescriptionProvider.

The source code in the following has a simple wrapper around a
PropertyDescriptor (ChainingPropertyDescriptor):
http://www.codeproject.com/csharp/HyperPropertyDescriptor.asp

It also has various details on implementing a
TypeDescriptionProvider - just ignore the Reflection.Emit stuff if you
aren't interested.

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