PropertyGrid is a very useful control in C# that displays the properties of the specified control and allows to change them during the runtime. When a control is specified to the PropertyGrid, it displays all the properties, even those inherited from the base class of that control. However, at many times, you may not want to display those properties as they do not make sense or are irrelevant. Alternatively, you may want to display some of the inherited ones but not all of them. So, basically what you need is a filtering tool that filters the properties out of the PropertyGrid.

Implementing such a filter mechanism is quiet simple. An example of doing so is provided by Bob Powell and is available from the following location.

http://www.devnewsgroups.net/windowsforms/t4152-how-hide-objects-inherited-properties.aspx

The example shows a filter that works based on the ‘Name’ of the property. But, this can be done based on the ‘Category’ of the property as well.

You may look at following sample that performs the filter based on ‘Category’. This code is based upon the sample in above location.

The most important thing to consider is that your custom control class should be derived from ICustomTypeDescriptor interface. And that, you need to specify a well describing ‘Category’ for your properties. The filtering magic is done by the FilterProperties(…) method.

class CustomControl1 : BaseControl, ICustomTypeDescriptor
{
#region Properties

private Font _groupHeaderFont;
[Category("CustomControl1_Category"), DescriptionAttribute("Set the Font of the Group Headers.")]
public Font GroupHeaderFont
{
get
{
return _groupHeaderFont;
}
set
{
if (value != _groupHeaderFont)
{
_groupHeaderFont = value;
}
}
}

private Color _selectionColor = Color.FromArgb(225, 230, 232);
[Category("CustomControl1_Category"), DescriptionAttribute("Set the selection color for the items.")]
public Color SelectionColor
{
get
{
return _selectionColor;
}
set
{
if (_selectionColor != value)
{
_selectionColor = value;
}
}
}

#endregion

#region Customize Property Grid
private string[] CategoriesToShow = { "CustomControl1_Category" };

//Does the property filtering...
private PropertyDescriptorCollection FilterProperties(PropertyDescriptorCollection pdc)
{
ArrayList toShow = new ArrayList();
foreach (string s in CategoriesToShow)
toShow.Add(s);

PropertyDescriptorCollection adjustedProps =
new PropertyDescriptorCollection(new PropertyDescriptor[] { });

foreach (PropertyDescriptor pd in pdc)
if (toShow.Contains(pd.Category))
adjustedProps.Add(pd);

return adjustedProps;
}

#region ICustomTypeDescriptor Members

public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}

public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}

EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}

public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}

public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}

public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this,
attributes, true);
return FilterProperties(pdc);
}

PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this,
true);
return FilterProperties(pdc);
}

public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}

public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}

public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}

public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}

#endregion

//...
//Other codes ...
}

When a PropertyGrid is assigned this control, it will display only the properties that fall under the category ‘CustomControl1_Category’. In above example, these properties are ‘GroupHeaderFont’ and ‘SelectionColor’.



4 Responses to “Hide properties inherited from Base class in the PropertyGrid”  

  1. Hey, I found your blog while searching on Google your post looks very interesting for me. I will add a backlink and bookmark your site. Keep up the good work!

  2. There is obviously a lot to know about this. There are some good points here. :)

  3. There’s good info here. I did a search on the topic and found most people will agree with your blog. Keep up the good work mate! :)

  4. I don’t know If I said it already but …Cool site, love the info. I do a lot of research online on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, :)

    A definite great read….


Leave a Reply