views:

264

answers:

2

I'm using a PropertyGrid in an application I am writing to allow users to view and sometimes edit instances of my objects. Sometimes the user may have a file open in read/write mode where they can make changes to the file through the property grid. In other cases they may have a file open in read only mode, and should not be able to make any changes to the objects through the PropetyGrid. My classes also have dynamic properties which are returned by implementing ICustomTypeDescriptor. Which is why I really want to take advantage of the built in flexibility of a PropertyGrid control.

There doesn't seem to be an easy way to set a Property-grid to a read only mode. If I disable a PropertyGrid this also prevents the user from scrolling the list. So I'm thinking the best way to do this is to add ReadOnlyAttributes to the properties at run-time. Is there some other way?

A: 

My advice would be to write a custom control that inherits from the propertygrid control, and in that custom control, have a boolean value of readonly, and then override some things and check, if(readonly) then cancel the action

Tommy
+1  A: 

Since you are implementing ICustomTypeDescriptor there is no need to add any attributes; you can just override IsReadOnly on the PropertyDescriptor. I'm thinking it should be pretty simple to write an intermediary type that mimics (via ICustomTypeDescriptor and TypeConverter) a wrapped type but always returns readonly PropertyDesciptor instances? Let me know if you want an example (it isn't trivial though).

You might also want to check whether something like this offers it built it.

Marc Gravell
Yeah this is what I was thinking but was hoping their was an easier way. The real problem is sometimes my class is read only and other times it is not. Rather than creating a separate read only wrapper type what about just adding a boolean property to the base type that ICustomTypeDescriptor.GetProperties() checks to see if it should be returning editable or read only properties?
Eric Anastas