Click here to Skip to main content
Click here to Skip to main content
Go to top

Enabling/disabling properties at runtime in the PropertyGrid

, 3 Feb 2011
Rate this:
Please Sign up or sign in to vote.
How to enable/disable properties at runtime in the PropertyGrid.

Introduction

Many people know about the benefits of using PropertyGrids in user interfaces.

You probably know about the System.ComponentModel namespace, and how it can help you in customizing the behavior of classes and their properties when edited through controls like the PropertyGrid. One of the attributes you can find in that namespace is ReadOnly, which will make a property to be read only and appear in gray (even if it has the Set accessor).

The problem with this attribute is that it must be defined at build-time. Sometimes, it is very useful (and improves the user experience quite a bit), to enable or disable properties at runtime.

Imagine the following example, where we have a “UserData” class:

public class UserData
{
    ...

    public string Country
    {
        get { return mCountry; }
        set { mCountry = value; }
    }
    public string State
    {
        get { return mState; }
        set { mState = value; }
    }
}

Now, imagine you want the user to fill the State field only in the case he selects US as his Country. In such a case, you need to make this change at Runtime.

There are several solutions for this, but I find the following one to be the most elegant, as it's coded in the UserData class itself. The final code looks like:

[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
[ReadOnly(false)]
public string Country
{
  get { return mCountry; }
  set
      {
      mCountry = value;
      PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["State"];
      ReadOnlyAttribute attribute = (ReadOnlyAttribute)
                                    descriptor.Attributes[typeof(ReadOnlyAttribute)];
      FieldInfo fieldToChange = attribute.GetType().GetField("isReadOnly", 
                                       System.Reflection.BindingFlags.NonPublic | 
                                       System.Reflection.BindingFlags.Instance);
      fieldToChange.SetValue(attribute, mCountry != "U.S.");
      }
}

[ReadOnly(true)]
public string State
{
   get { return mState; }
   set { mState = value; }
}

Some Tips

  1. We obviously change the ReadOnly attribute of the State property in the setter accessor of the property Country. That's the exact point where we know if State should be enabled or disabled.
  2. It is important to add the RefreshProperties attribute to the Country property. That will force the PropertyGrid control to refresh all its properties every time the value of Country changes, reflecting the changes we made to the attributes of State.
  3. In order for all this to work properly, it is important to statically define the ReadOnly attribute of every property of the class to whatever value you want. If not, changing the attribute at runtime that way will wrongly modify the attributes of every property of the class.

Hope it helps!

More Info

Cheers!

License

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

Share

About the Author

Inaki Ayucar
Software Developer (Senior)
Spain Spain
Inaki Ayucar is a Microsoft MVP in DirectX/XNA, and a software engineer involved in development since his first Spectrum 48k, in the year 1987. He is the founder and chief developer of The Simax Project (www.simaxvirt.com) and is very interested in DirectX/XNA, physics, game development, simulation, C++ and C#.
 
His blog is: http://graphicdna.blogspot.com
 
To contact Inaki: iayucar@simax.es

Comments and Discussions

 
GeneralVote of 5 PinprofessionalLee Zhe Heng28-Nov-13 19:18 
GeneralMy vote of 5 PinmemberStefano Manni10-Oct-12 21:24 
GeneralMy vote of 5 PinmemberMember 41913665-Jun-12 20:03 
QuestionIn case of a collection of UserData objects. PinmemberSwiebertje26-May-11 5:53 
QuestionAsk question for enable/disable article [modified] PinmemberSports Kuo23-Feb-11 0:34 
AnswerRe: Ask question for enable/disable article [modified] PinmemberMember 1070314726-Jun-14 2:56 
GeneralMy vote of 5 PinmemberMrRexx22-Feb-11 21:28 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 3 Feb 2011
Article Copyright 2011 by Inaki Ayucar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid