Search code examples
c#winformspropertygridreadonly-attribute

PropertyGrid - Dynamically changing ReadOnlyAttribute


Sigh, another PropertyGrid question. I thought I could get around this until I ran into a problem where I couldn't actually avoid it.

I have a boolean property that sometimes needs to be read-only and sometimes needs to be changeable depending on the object selected from a TreeView.

My question is how can I change the ReadOnlyAttribute of a property dynamically? Obviously, creating a boolean variable and then trying to set it like ReadOnlyAttribute(boolVar) doesn't work and now I'm out of ideas.

The only solution I can think of is creating separate, near-identical classes for items where this property is writable and one for read-only, but this seems a bit unelegant to me.

Help? :)


Solution

  • What I would do is create a base class with a protected version the property, then create two classes that inherit the base class that have the readonly and the non-readonly bits.