VisualHint's blog

How to filter out base classes properties from the PropertyGrid

I like to post some howtos or tips when the initial concern comes from a customer or visitor. One of them in the forums asked if it was possible to filter out properties while using SelectedObject and even if all properties from ancestor classes could be removed automatically. Well, yes, it’s feasible in Smart PropertyGrid. This is very quick. Here is how:

Let's say you have a class MyControl derived from the Control class.

You must first add a handler for the PropertyPreFilterOut event or simply override OnPropertyPreFilterOut in a PropertyGrid derived class:

protected override void OnPropertyPreFilterOut(PropertyPreFilterOutEventArgs e)
    // Write some code here

Now to remove all the properties coming from ancestor classes, we must pay attention to their PropertyDescriptor and target instance so that we keep all properties published by our MyControl derived class and all their descendants:

PropertyDescriptor pd = e.PropertyDescriptor;
if ((pd.ComponentType == typeof(MyControl)) || (e.TargetInstance.GetType() != typeof(MyControl)))
    e.FilterOut = PropertyPreFilterOutEventArgs.FilterModes.FilterDefault;
    e.FilterOut = PropertyPreFilterOutEventArgs.FilterModes.FilterOut;