C# Enabling/Disabling fields in PropertyGrid

cpropertygrid

I am developing a user control library, in which I need to provide programmer, a property grid to customize the properties of my control.
If the programmer uses System.Windows.Forms.PropertyGrid (or Visual Studio's Designer)
some of the property fields in the System.Windows.Forms.PropertyGrid should be enabled/disabled depending on some other property of the same user control.
How to do it?

Example Scenario

This is not actual example, but an illustration only.
Ex: UserControl1has two custom properties:
MyProp_Caption: a string
and
MyProp_Caption_Visible: a bool
Now, MyProp_Caption should be enabled in the PropertyGrid only when MyProp_Caption_Visible is true.

Sample Code for the UserControl1

public class UserControl1: UserControl <br/>
{
    public UserControl1()
    {
        // skipping details
        // label1 is a System.Windows.Forms.Label
        InitializeComponent();
    }
    [Category("My Prop"), 
    Browsable(true), 
    Description("Get/Set Caption."), 
    DefaultValue(typeof(string), "[Set Caption here]"), 
    RefreshProperties(RefreshProperties.All), 
    ReadOnly(false)]
    public string MyProp_Caption
    {
        get
        {
            return label1.Text;
        }
        set
        {
            label1.Text = value;

        }
    }
    [Category("My Prop"), 
    Browsable(true), 
    Description("Show/Hide Caption."), 
    DefaultValue(true)]
    public bool MyProp_Caption_Visible
    {
        get
        {
            return label1.Visible;
        }
        set
        {
            label1.Visible = value;
            // added as solution:
            // do additional stuff to enable/disable 
            // MyProp_Caption prop in the PropertyGrid depending on this value 
            PropertyDescriptor propDescr = TypeDescriptor.GetProperties(this.GetType())["MyProp_Caption"];
            ReadOnlyAttribute attr = propDescr.Attributes[typeof(ReadOnlyAttribute)] as ReadOnlyAttribute;
            if (attr != null)
            {
                 System.Reflection.FieldInfo aField = attr.GetType().GetField("isReadOnly", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                 aField.SetValue(attr, !label1.Visible);
            }            
        }
    }
}

Sample Code for PropertyGrid on this UserControl1

  • tstFrm is a simple Form with the following two data members

        private System.Windows.Forms.PropertyGrid propertyGrid1;
        private UserControl1 userControl11;
    

And we can customize userControl1 through propertyGrid1 as below:

public partial class tstFrm : Form
{
    public tstFrm()
    {
        // tstFrm embeds a PropertyGrid propertyGrid1
        InitializeComponent();
        propertyGrid1.SelectedObject = userControl11;
    }
}

How to enable/disable the field MyProp_Caption in the property grid depending on the value of MyProp_Caption_Visible?

Best Answer

(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

Solved!

thanks to @Simon Mourier! Posting the edited code. Result:

ENABLEDDISABLED

Related Topic