Question

I need to use propertygrid in my project and I need to filter the attributes that will be shown. I found a way to filter the propertygrid by category but I need to go deeper when filtering it.

here is the code which only displays the "Appearance" category. But I need to disable some attributes under the "Appearance" like "BackColor"

Attribute myfilterattribute = new CategoryAttribute("Appearance");
pg_1.BrowsableAttributes = new AttributeCollection(new Attribute[] { myfilterattribute });

how can I filter out the Backcolor as well?

Was it helpful?

Solution

If you want to disable this props statically, e.g. at compile time, you can try this approach. Set them unvisible a run time is more complex, so take a look for dynamic property setting.

OTHER TIPS

I usually use this method tho show/hide the property dinamically in the PropertyGrid (using Reflection):

public void ShowValue(string _Who, bool _Enabled)
{
    BrowsableAttribute attribute = (BrowsableAttribute)TypeDescriptor.GetProperties(this.GetType())(_Who).Attributes(typeof(BrowsableAttribute));
    System.Reflection.FieldInfo fieldToChange = attribute.GetType().GetField("Browsable", Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Instance | Reflection.BindingFlags.Public | Reflection.BindingFlags.Static | Reflection.BindingFlags.IgnoreCase);
    fieldToChange.SetValue(attribute, _Enabled);
      //Refresh the Property Grid
    PG.Refresh();
}

The parameters are:

  • _Who : the name of property to change dynamically
  • _Enabled : true to show or false to hide the property from PropertyGrid

The name of PropertyGrid I am using is PG. I need to refresh it after the change.

This method should be in the class of being used as SelectedObject of PropertyGrid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top