Reputation: 5866
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?
Upvotes: 0
Views: 930
Reputation: 4699
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:
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.
Upvotes: 1
Reputation: 1786
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.
Upvotes: 1