In the development WinForm program, PropertyGrid is a common control, in the process of using PropertyGrid, you only need to assign the corresponding object instance to the Selectedobject property of the PropertyGrid. Of course, you need to add the corresponding attribute to the corresponding object definition.
1 classHuman2 {3 Private int_age;4 Private string_name;5 PrivateGender _gender;6 7[DisplayName ("name"),8Browsable (true)]9 Public stringNameTen { One Get{return_name;} A Set{_name =value;} - } -[DisplayName ("Age"), theBrowsable (true)] - Public int Age - { - Get{return_age;} + Set{_age =value;} - } + A[DisplayName ("Sex"), atBrowsable (true), -ReadOnly (true)] - PublicGender Gender - { - Get{return_gender;} - Set{_gender =value;} in } - to } + - enumGender the { * Mans, $ WomanPanax Notoginseng}
Human.cs
As you can see, users can customize the display of properties in PropertyGrid, including display names, and so on. Note the ReadOnly attribute in the code and the browsable attribute, where the values of these two attributes cannot be changed after compilation is complete.
However, we may have this requirement to dynamically change the ReadOnly feature and the browsable feature during program operation. This can be achieved by reflection, first the code
1 Public Partial classForm1:form2 {3Human _human =NULL;4 PublicForm1 ()5 {6 InitializeComponent ();7_human =NewHuman {Name ="Jashon Han", Gender = Gender.man, age = - };8Load + =Form1_Load;9 Ten } One A voidForm1_Load (Objectsender, EventArgs e) - { - This. Propertygrid1.selectedobject =_human; the } - - Private voidButtonreadonly_click (Objectsender, EventArgs e) - { +Setpropertyreadonly (_human,"Gender",true); - This. Propertygrid1.selectedobject =_human; + } A at Private voidButtonhide_click (Objectsender, EventArgs e) - { -Setpropertyvisibility (_human," Age",false); - This. Propertygrid1.selectedobject =_human; - } - in Private voidSetpropertyvisibility (ObjectObjstringPropertyName,BOOLvisible) - { to Try + { -Type type =typeof(BrowsableAttribute); thePropertyDescriptorCollection props =typedescriptor.getproperties (obj); *AttributeCollection attrs =Props[propertyname]. Attributes; $FieldInfo fld = type. GetField ("browsable", BindingFlags.Instance |bindingflags.nonpublic);Panax Notoginseng fld. SetValue (Attrs[type], visible); - } the Catch(Exception ex) + { A MessageBox.Show (ex. ToString ()); the } + } - $ Private voidSetpropertyreadonly (ObjectObjstringPropertyName,BOOLisreadonly) $ { - Try - { theType type =typeof(ReadOnlyAttribute); -PropertyDescriptorCollection props =typedescriptor.getproperties (obj);WuyiAttributeCollection attrs =Props[propertyname]. Attributes; theFieldInfo fld = type. GetField ("isreadonly", BindingFlags.Instance | BindingFlags.NonPublic |bindingflags.createinstance); - fld. SetValue (Attrs[type], isreadonly); Wu } - Catch(Exception ex) About { $ MessageBox.Show (ex. ToString ()); - } - -}
Form1.cs
The results are as follows:
Click the "Gender is read-only" button:
Click the "Hide Age" button:
Change the read-only and visible properties of a PropertyGrid by reflection