Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi,

I have a field in a class which the user can modify via a PropertyGrid.

So I made a public property which can be accessed via the PropertyGrid and the user can assign a value.

The problem is that I want to make sure that the value assigned by the user via the PropertyGrid should be readonly i.e. the property should not be modified through the object reference.

In other words I somehow want to make sure that user only can assign the value of the property via the PropertyGrid and anyone who is using this object should not be able to modify it directly.

This property should be readonly but from the PorpertyGrid one should be able to modify it. Is there any way to achieve this? Is there any way to modify private properties via the PropertyGrid?
Posted
Updated 26-Oct-10 4:07am
v2

1 solution

Unfortunately no. If the property grid can access it, then any other code can too.

But there is a trick you can use that's specific to the property grid. Remove the public property and replace it with a dynamic runtime property using ICustomTypeDescriptor and TypeDescriptionProvider. I've got an article here that explains how to do this:

http://www.codeproject.com/KB/cs/UsingTypeDescriptionProv.aspx[^]

It is not a very simple task but it's not too complex either.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900