How do you control what is visible in the property grid at runtime?

I have a property grid displaying a list, like the Person class

 [TypeConverter(typeof(ExpandableObjectConverter))] public class Person { public bool ShowHidden { get; set; } public string Name { get; set; } //[Browsable(false)] public string Hidden { get; set; } public override string ToString() { return string.Format("Person({0})", Name); } } 

The question is how to control the Browsable() attribute at runtime so that when ShowHidden = false the Hidden line (highlighted in yellow below) is omitted.

Screenshothot

Thanks.

+7
source share
1 answer

Here is an example:

 PropertyDescriptor descriptor= TypeDescriptor.GetProperties(this.GetType())["DataType"]; BrowsableAttribute attrib= (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)]; FieldInfo isBrow = attrib.GetType().GetField("browsable",BindingFlags.NonPublic | BindingFlags.Instance); isBrow.SetValue(attrib,false); 

Just replace the DataType with your property name. Note that all properties must have a mutable attribute (in this case, Browsable). If one of the properties does not have an attribute, all the class properties gets a new attribute.

Code taken from here: Exploring the behavior of a property grid .

+10
source

All Articles