Click here to Skip to main content
13,554,046 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 27 Jun 2010
Licenced CPOL

Making a PropertyGrid ReadOnly

, 6 Jul 2010
Rate this:
Please Sign up or sign in to vote.
The System.Windows.Forms.PropertyGrid does not support displaying the selected objects as read only. This article describes the way to do it.


The PropertyGrid is one of the most useful controls provided in System.Windows.Forms namespace. However, one important feature missing in it is the ability to display the selected object in a read only format. This article proposes a method to achieve this by the means of a control that inherits PropertyGrid and wraps the selected object in a custom wrapper to bring about the read only effect.

Understanding the Terms Used in this Article

Throughout this article, you will come across terms like PropertyGrid, TypeDescriptor, ICustomTypeDescriptor, PropertyDescriptor, etc. If you feel comfortable with these terms and understand their roles, it would be easier to understand the article. If not, no worries. You may directly skip to the how to use section. The knowledge of these terms or their working is not necessary to be able to use the solution. The attached binaries include the XML documentation to make it easier to use the properties and methods of the proposed control.


Nearly two years ago, while working on a designer I had faced a situation where the PropertyGrid was expected to display the selected object as read only in certain situations. But the control provided by the framework has no provision for the same. So, while searching for the possible solutions, I came across a lot of forums where others needed a similar behavior but no elegant solution was proposed. The most widely used workarounds were:

  1. Simply disabling the control when the read only behavior was needed

    Problems: Disabling the control is not only a lazy substitute, but also a visually unpleasant one. When the control is disabled, it loses its ability to scroll, thereby rendering it pretty useless when it comes to browsing through the properties. It also makes the grids un-expandable, so viewing the child properties is impossible.

  2. Implementing ICustomTypeDescriptor for the classes whose objects need to be displayed as selected object

    Problems: Implementing ICustomTypeDescriptor does empower the type to tailor its properties and manipulate their attributes. However this ability is restricted to the implementing class and has no power over the child properties. And it is not always possible to implement ICustomTypeDescriptor for all the classes that may make up the properties of the root object, because they may be a part of the framework's classes. Thus this does not provide a fool proof and complete solution. Some also propose to disable the ability to expand the child properties. But this, again, makes browsing the properties a big pain.

So, after playing around with PropertyGrid, TypeConverters, TypeDescriptors, etc. I had discovered the following solution then. And recently I realized that this, or a similar solution, was not yet available to everyone (at least my search for it proved futile) I decided to publish it here.

Proposed Solution

The solution proposed in this article involves the use of a new control called RPropertyGrid that inherits from PropertyGrid and has a property called ReadOnly which can be used to make the selected object read only.

Internally, when the ReadOnly property is set to true the control wraps the selected objects by instances of a custom class called the ReadOnlyWrapper, which makes the object behave as if they were read only.

Behind the Scene

When the selected object is wrapped by the ReadOnlyWrapper, the following components come into play.

ReadOnlyWrapper: This is the class where the process of making the object seem read only happens. It implements ICustomTypeDescriptor, but most of the implemented methods return the values obtained by calling their counterparts provided by the TypeDescriptor class on the wrapped object. The only method that does not do this is the GetProperties method which in turn wraps the original properties (instances of PropertyDescriptor) by instances of ReadOnlyPropertyDescriptor.

ReadOnlyPropertyDescriptor: This is the class that serves as a proxy property descriptor and its ReadOnly property always returns true. This makes them appear as read only in the PropertyGrid. The other two aberrations in this class are in the form of the property Converter and the method GetEditor which always return instances of ReadOnlyConverter and UITypeEditor respectively.

ReadOnlyConverter: This is the class that derives from TypeConverter and acts on behalf of the original converter of the wrapped object. In most cases, it simply returns the actual values obtained by using the counterpart methods of the original converter. However, the GetProperties method, again, is the only point where the behavior changes. In the custom implementation of this method, instead of returning the original PropertyDescriptor of the properties, they are wrapped in instances of ReadOnlyPropertyDescriptor, thus making the child properties read only too.

Since ReadOnlyPropertyDescriptor uses ReadOnlyConverter and vice versa, all the properties of the root object and the subsequent properties of its child properties are made read only. This "symbiotic" mechanism forms the core of the idea. Also, since all the wrapping and unwrapping happens at the PropertyDescriptor and TypeConverter levels, it makes no difference if the wrapped object is a custom object or one that instantiates some built-in class.

How to Use RPropertyGrid

There is no need to make any change in your existing components to be able to use the given solution. Your classes need not implement any special interface or derive from any special class.

RPropertyGrid derives from PropertyGrid, so it can simply replace your existing control without demanding any change in the way selected objects are set. Here is an example (with your original code shown in comments).

//PropertyGrid propertyGrid = new PropertyGrid(); 
RPropertyGrid propertyGrid = new RPropertyGrid();
propertyGrid.SelectedObject = value;   

Moreover, it provides three new properties which have the following use:

  • ReadOnly: Property that lets you set (and, of course, get) the selected objects as read only. Everything that needs to be done is internally handled.
  • OriginalSelectedObject: Property that lets you get the original selected object, in case you need to retrieve the actual object that was used as the selected object (and not the custom wrapper's instance).
  • OriginalSelectedObjects: Property that lets you get the original selected objects, in case you need to retrieve the actual objects that were used as the selected objects (and not the custom wrapper's instances).


When the ReadOnly property is set to true, the UITypeEditors of the properties are stopped from being shown. Generally this is not a problem because properties with a UITypeEditor typically have a TypeConverter associated with them that show their state well enough. However, in rare cases where an object can only be described by the means of an editor (e.g. a pop-up editor that could show an image for a property of type Image), this limitation would apply.

Just a Side Note

I intend to add some more controls to this assembly in the future, and hence the audacious name. Please excuse me for it!


  • June 28, 2010: Article submitted
  • July 7, 2010: Made correction to stop the extra firing of 'SelectedObjectsChanged' event


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


About the Author

Software Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralAnother approach Pin
Yumashin Alex7-Jan-13 3:39
memberYumashin Alex7-Jan-13 3:39 
QuestionExcellent work ! One question ... Pin
BillWoodruff5-Jan-13 18:18
memberBillWoodruff5-Jan-13 18:18 
QuestionIt works fine for me Pin
mchwalt23-Jul-12 2:23
membermchwalt23-Jul-12 2:23 
GeneralRe: It works fine for me Pin
rajeev51128-Aug-12 16:52
memberrajeev51128-Aug-12 16:52 
GeneralAnother alternative Pin
David Catriel29-Apr-11 16:59
memberDavid Catriel29-Apr-11 16:59 
GeneralRe: Another alternative Pin
rajeev51129-Apr-11 19:25
memberrajeev51129-Apr-11 19:25 
GeneralMy vote of five Pin
David Catriel28-Apr-11 15:23
memberDavid Catriel28-Apr-11 15:23 
GeneralMy vote of 5 Pin
David Catriel28-Apr-11 15:22
memberDavid Catriel28-Apr-11 15:22 
GeneralRe: My vote of 5 Pin
rajeev51128-Apr-11 16:42
memberrajeev51128-Apr-11 16:42 
GeneralCombined approach Pin
hmoeller.privat7-Jul-10 3:57
memberhmoeller.privat7-Jul-10 3:57 
GeneralRe: Combined approach Pin
rajeev5117-Jul-10 23:12
memberrajeev5117-Jul-10 23:12 
GeneralMy vote of 1 Pin
msmits6-Jul-10 20:19
membermsmits6-Jul-10 20:19 
GeneralRe: My vote of 1 Pin
rajeev5117-Jul-10 6:26
memberrajeev5117-Jul-10 6:26 
GeneralSelectedObjectsChanged event is triggered twice Pin
lugreen6-Jul-10 17:04
memberlugreen6-Jul-10 17:04 
GeneralRe: SelectedObjectsChanged event is triggered twice Pin
rajeev5116-Jul-10 18:52
memberrajeev5116-Jul-10 18:52 
GeneralRe: SelectedObjectsChanged event is triggered twice Pin
lugreen6-Jul-10 23:39
memberlugreen6-Jul-10 23:39 
GeneralRe: SelectedObjectsChanged event is triggered twice Pin
rajeev5117-Jul-10 6:16
memberrajeev5117-Jul-10 6:16 
GeneralMy vote of 5 Pin
lugreen5-Jul-10 17:20
memberlugreen5-Jul-10 17:20 
GeneralRe: My vote of 5 Pin
rajeev5115-Jul-10 19:19
memberrajeev5115-Jul-10 19:19 
GeneralAnother way Pin
tonyt28-Jun-10 2:17
membertonyt28-Jun-10 2:17 
GeneralRe: Another way Pin
rajeev51128-Jun-10 6:26
memberrajeev51128-Jun-10 6:26 
GeneralRe: Another way Pin
tonyt28-Jun-10 23:38
membertonyt28-Jun-10 23:38 
GeneralRe: Another way Pin
rajeev51129-Jun-10 14:31
memberrajeev51129-Jun-10 14:31 
GeneralRe: Another way [modified] Pin
tonyt3-Jul-10 12:42
membertonyt3-Jul-10 12:42 
GeneralRe: Another way Pin
rajeev5114-Jul-10 10:40
memberrajeev5114-Jul-10 10:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180515.1 | Last Updated 7 Jul 2010
Article Copyright 2010 by rajeev511
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid