title: C# Enabling/Disabling fields in PropertyGrid
body: <p>I am developing a user control library, in which I need to provide programmer, a property grid to customize the properties of my control.<br/>
  If the programmer uses <code>System.Windows.Forms.PropertyGrid</code> (or Visual Studio's Designer)<br/> some of the property fields in the <code>System.Windows.Forms.PropertyGrid</code> should be enabled/disabled depending on some other property of the same user control.<br/>
  How to do it?<br/></p>
  
  <h2>Example Scenario</h2>
  
  <p>This is not actual example, but an illustration only.<br>
  Ex: <code>UserControl1</code>has two custom properties:<br/>
  <strong>MyProp_Caption</strong>: a string <br/> and <br/>
  <strong>MyProp_Caption_Visible</strong>: a bool<br/>
  Now, <strong>MyProp_Caption</strong> should be enabled in the PropertyGrid only when <strong>MyProp_Caption_Visible</strong> is true.</p>
  
  <h2>Sample Code for the UserControl1</h2>
  
  <pre><code>public class UserControl1: UserControl &lt;br/&gt;
  {
      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);
              }            
          }
      }
  }
  </code></pre>
  
  <h2>Sample Code for PropertyGrid on this UserControl1</h2>
  
  <ul>
  <li><p>tstFrm is a simple Form with the following two data members</p>
  
  <pre><code>    private System.Windows.Forms.PropertyGrid propertyGrid1;
      private UserControl1 userControl11;
  </code></pre></li>
  </ul>
  
  <p>And we can customize userControl1 through propertyGrid1 as below:</p>
  
  <pre><code>public partial class tstFrm : Form
  {
      public tstFrm()
      {
          // tstFrm embeds a PropertyGrid propertyGrid1
          InitializeComponent();
          propertyGrid1.SelectedObject = userControl11;
      }
  }
  </code></pre>
  
  <p><strike>How to enable/disable the field MyProp_Caption in the property grid depending on the value of MyProp_Caption_Visible?</strike> <br/></p>