编写一个对象,后面传递给 PropertyGrid 来显示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace WindowsForms_PropertyGrid {public class UserPropertyClass{private string appVersion = "1.0";[CategoryAttribute("版本"),DefaultValueAttribute("1.0"),ReadOnlyAttribute(true)]public string AppVersion{get { return appVersion; }set { appVersion = value; }}private Buchang _ReadWrite_06=Buchang .标准溶液;[CategoryAttribute("读写存储区"), DisplayName("06 补偿类型")]public Buchang ReadWrite_06{get {switch (BuchangValue){case 1:_ReadWrite_06 = Buchang.标准溶液;break;case 2:_ReadWrite_06 = Buchang.自然水;break;case 3:_ReadWrite_06 = Buchang.纯水;break;case 4:_ReadWrite_06 = Buchang.其他溶液;break;default:break;}return _ReadWrite_06; }set{switch (value){case Buchang.标准溶液:_BuchangValue = 1;break;case Buchang.自然水:_BuchangValue = 2;break;case Buchang.纯水:_BuchangValue = 3;break;case Buchang.其他溶液:_BuchangValue = 4;break;default:break;}_ReadWrite_06 = value;}}private int _BuchangValue = 1;public int BuchangValue{get{return _BuchangValue;}set{switch (value){case 1:ReadWrite_06 = Buchang.标准溶液;break;case 2:ReadWrite_06 = Buchang.自然水;break;case 3:ReadWrite_06 = Buchang.纯水;break;case 4:ReadWrite_06 = Buchang.其他溶液;break;default:break;}_BuchangValue = value;}}public enum Buchang{标准溶液 = 0,自然水 = 1,纯水 = 2,其他溶液 = 3,}} }
Winform 界面代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WindowsForms_PropertyGrid {public partial class MainForm : Form{public MainForm(){InitializeComponent();}UserPropertyClass UserPropertyClass1;// = new UserPropertyClass();private void MainForm_Load(object sender, EventArgs e){UserPropertyClass1 = new UserPropertyClass();UserPropertyClass1.BuchangValue = 4;propertyGrid1.SelectedObject = UserPropertyClass1;timer1.Enabled = true;}private void timer1_Tick(object sender, EventArgs e){label2.Text = UserPropertyClass1.BuchangValue.ToString ();}private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e){propertyGrid1.Refresh();}} }
运行后显示效果: