PropertyGrid: Hide base class properties, how?

user834850 picture user834850 · Aug 14, 2011 · Viewed 10.7k times · Source

PropertyGrid... for users Id like to leave only several of them. But now I see all, and users would be confused when see something like Dock or Cursor and such... Hope it's clear for now...

Answer

LarsTech picture LarsTech · Aug 14, 2011

Use this attribute:

[Browsable(false)]
public bool AProperty {...} 

For the inherited properties:

[Browsable(false)]
public override bool AProperty {...} 

Another idea (since you are trying to hide all base class members):

public class MyCtrl : TextBox
{
  private ExtraProperties _extraProps = new ExtraProperties();

  public ExtraProperties ExtraProperties
  {
    get { return _extraProps; }
    set { _extraProps = value; }
  }
}

public class ExtraProperties
{
  private string _PropertyA = string.Empty;

  [Category("Text Properties"), Description("Value for Property A")]
  public string PropertyA {get; set;}

  [Category("Text Properties"), Description("Value for Property B")]
  public string PropertyB { get; set; }
}

and then for your property grid:

  MyCtrl tx = new MyCtrl();
  pg1.SelectedObject = tx.ExtraProperties;

The down side is it changes your access level of those properties from

tx.PropertyA = "foo";

to

tx.ExtraProperties.PropertyA = "foo";