ReadOnlyAttribute

J

John Allen

Hi there,

Given a "PropertyGrid" with one particular property that appears as follows:

-Size
Width 10
Height 20

where "Size" is the native .NET "Size" structure, if the property is marked with the "ReadOnlyAttribute" then all three lines above appear grayed out as usual. However, if you replace "Size" with your own customized "Size" structure" instead (not inherited from the native .NET "Size" structure), then "Size" above will now be grayed out but not the "Width" and "Height" lines. Is there no way to propagate the "ReadOnlyAttribute" to all children or do we really have to handle this manually (by creating a read-only "PropertyDescriptor" for all children). Thanks.
 
J

John Allen

Sorry, I should clarify that the read-only attribute is applied at runtime (on-the-fly) depending on conditions.
 
J

Jon Skeet [C# MVP]

John Allen said:
Sorry, I should clarify that the read-only attribute is applied at
runtime (on-the-fly) depending on conditions.

How are you applying attributes at runtime? As far as I'm aware,
whether or not an attribute is applied to something is a compile-time
decision, and can't be changed at runtime.
 
J

John Allen

How are you applying attributes at runtime? As far as I'm aware,
whether or not an attribute is applied to something is a compile-time
decision, and can't be changed at runtime.

You can create your own "PropertyDescriptor" objects at runtime and take
complete control over your properties including their attributes (augmenting
or overriding whatever you want). See
"PropertyDescriptor.CreateAttributeCollection()" and
"PropertyDescriptor.AttributesArray" for instance. Note that you can can
take control over all types in general using the
"TypeDescriptionProviderAttribute" (for 2.0 and later),
"ICustomTypeDescriptor" and (to some extent) "TypeConverterAttribute". The
documentation to figure this all out is no picnic however.
 
J

John Allen

BTW, "PropertyDescriptor.IsReadOnly", "PropertyDescriptor.IsBrowsable", etc.
complicate things even further (noting that the latter property isn't event
respected by the "PropertyGrid" but this is likely a bug and I've reported
it to MSFT).
 
J

Jon Skeet [C# MVP]

John Allen said:
You can create your own "PropertyDescriptor" objects at runtime and take
complete control over your properties including their attributes (augmenting
or overriding whatever you want). See
"PropertyDescriptor.CreateAttributeCollection()" and
"PropertyDescriptor.AttributesArray" for instance. Note that you can can
take control over all types in general using the
"TypeDescriptionProviderAttribute" (for 2.0 and later),
"ICustomTypeDescriptor" and (to some extent) "TypeConverterAttribute". The
documentation to figure this all out is no picnic however.

Ah, I see - not like the normal way of applying attributes at all.

If your property *starts off* read-only, is it greyed out in the
property grid? I wonder whether it's not noticing the change.
 
S

Stoitcho Goutsev \(100\)

John you need to refresh the property gred after you change the attribute. Try applying RefreshPropertiesAttrubute as well.
 
J

John Allen

Jon Skeet said:
Ah, I see - not like the normal way of applying attributes at all.

Yes, and I think the only time you typically have to resort to this is when
you need to get granular control over a "PropertyGrid" at runtime (there is
no other way AFAIK and the techniques are documented by both MSFT and others
of course). Figuring out all the details is a huge pain however (and it
takes an inordinate amount of code to implement ths simplest change). Is it
just me or are the .NET docs in general very weak?
If your property *starts off* read-only, is it greyed out in the
property grid? I wonder whether it's not noticing the change.

If I simply apply the read-only attribute to my customized "Size" property
then it grays it out but not its sub-properties ("Width" and "Height"). You
would think that it would but then again, my "Size" property is actually a
member of a larger object which I'm customizing the properties for (using
"PropertyDescriptor" objects). I see no reason why the read-only attribute
shouldn't be propagated to children however (without manual intervention
since again, it takes a lot of code to do this).
 
J

John Allen

Thanks. I'm already applying it with no effect however. Note that it's not grayed out even if I apply the read-only attribute at compile time (to the "Size" property). Since I'm customizing "PropertyDescriptor" objects at a higher level however, that may be impacting things but I don't know why. It seems counter-intuitive.to me (I think it should work regardless IOW).
John you need to refresh the property gred after you change the attribute. Try applying RefreshPropertiesAttrubute as well.
 
J

Jon Skeet [C# MVP]

John Allen said:
If I simply apply the read-only attribute to my customized "Size" property
then it grays it out but not its sub-properties ("Width" and "Height"). You
would think that it would but then again, my "Size" property is actually a
member of a larger object which I'm customizing the properties for (using
"PropertyDescriptor" objects). I see no reason why the read-only attribute
shouldn't be propagated to children however (without manual intervention
since again, it takes a lot of code to do this).

Is your Size type definitely a struct and not a class? I wonder if that
would make a difference - because strictly speaking, you could change
the contents of a reference type object without changing which object
was being referred to.
 
J

John Allen

Is your Size type definitely a struct and not a class? I wonder if that
would make a difference - because strictly speaking, you could change
the contents of a reference type object without changing which object
was being referred to.

Yes, it is a struct. I'm still experimenting but will likely toss in the
towel and manually code things if I can't resolve it soon. Thanks again for
your help.
 
J

Jon Skeet [C# MVP]

John Allen said:
Yes, it is a struct. I'm still experimenting but will likely toss in the
towel and manually code things if I can't resolve it soon. Thanks again for
your help.

No problem (not that my help has been anything!) - but it does sound
interesting.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

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