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

后端 未结 1 1842
余生分开走
余生分开走 2021-02-06 02:51

I have a property grid displaying a list, for example of a class Person

[TypeConverter(typeof(ExpandableObjectConverter))]
public class Person
{
            


        
1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 03:14

    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 DataType with your property name. Note, all properties must have the attribute being changed (in this case, Browsable). If one of the properties is missing the attribute, all of the class properties get the new attribute setting.

    Code taken from here: Exploring the Behaviour of Property Grid.

    0 讨论(0)
提交回复
热议问题