C# PropertyGrid使用总结
1. 只有public的property能显示出来,可以通过BrowsableAttribute来控制是否显示,通过CategoryAttribute设置分类,通过DescriptionAttribute设置描述,Attribute可以加在Class上,也可以加在属性上,属性上的Attribute优先级更高;
2. enum会自动使用列表框表示;
3. 自带输入有效性检查,如int类型输入double数值,会弹出提示对话框;
4. 基本类型Array:增加删除只能通过弹出的集合编辑器,修改可以直接展开,值为null时,可以通过集合编辑器创建;
5. 基本类型List:增删改都只能通过集合编辑器,值为null时,能打开集合编辑器,但不能保存结果,所以必须初始化;
6. ReadOnlyAttribute设置为true时,显示为灰色;对List无效,可以打开集合编辑器,在集合编辑器内可以进行增删改;应用于Array时,不能打开集合编辑器,即不能增删,但可以通过展开的方式修改Array元素;
7. 对象:值显示Object.ToString();Class上加了[TypeConverter(typeof(ExpandableObjectConverter))]之后(也可以加在属性上),才能展开编辑,否则显示为灰色只读;不初始化什么也干不了;
8. 基类类型派生类对象:与普通对象没有区别,[TypeConverter(typeof(ExpandableObjectConverter))]加在基类上即可;
9. 对象Array:增加删除只能通过弹出的集合编辑器,修改可以直接展开,值为null时,可以通过集合编辑器创建;
10. 对象List:增加删除修改只能通过弹出的集合编辑器,值为null时,能打开集合编辑器,但不能保存结果,所以必须初始化;
11. Hashtable和Dictionary能打开集合编辑器,但不能编辑;
12. 通过MyDoubleConverter实现Double类型只显示小数点后两位:
public class MyDoubleConverter : DoubleConverter { public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { return string.Format("{0:0.00}", value); } return base.ConvertTo(context, culture, value, destinationType); } }
13. 通过TypeConverter实现用字符串表示对象
[TypeConverter(typeof(StudentConverter))] public class Student { public Student(string name, int age) { Name = name; Age = age; } public string Name { get; set; } public int Age { get; set; } public static Student FromString(string s) { string name = "Default"; int age = 0; string[] splits = s.Split(','); if (splits.Length == 2) { name = splits[0]; int.TryParse(splits[1], out age); } return new Student(name, age); } public override string ToString() { return string.Format("{0},{1}", Name, Age); } } public class StudentConverter : TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { Student result = null; if ((value as string) != null) { result = Student.FromString(value as string); } return result; } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) { return true; } return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { return value.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } }
14. 使用UITypeEditor实现文件或目录选择
// 需要在项目里添加引用:程序集|框架|System.Design [Editor(typeof(FileNameEditor), typeof(UITypeEditor))] public string FileName { get; set; } [Editor(typeof(FolderNameEditor), typeof(UITypeEditor))] public string FolderName { get; set; }
15. 使用自定义的下拉式编辑界面
public class MyAddress { public string Province { get; set; } public string City { get; set; } public override string ToString() { return string.Format("{0}-{1}", Province, City); } } public class MyAddressEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; if (service != null) { service.DropDownControl(new MyEditorControl(value as MyAddress)); } return value; } } // 实现两个ComboBox用来编辑MyAddress的属性 public partial class MyEditorControl : UserControl { private MyAddress _address; public MyEditorControl(MyAddress address) { InitializeComponent(); _address = address; comboBoxProvince.Text = _address.Province; comboBoxCity.Text = _address.City; } private void comboBoxProvince_SelectedIndexChanged(object sender, EventArgs e) { _address.Province = comboBoxProvince.Text; } private void comboBoxCity_SelectedIndexChanged(object sender, EventArgs e) { _address.City = comboBoxCity.Text; } } // MyAddress属性声明 [Editor(typeof(MyAddressEditor), typeof(UITypeEditor))] public MyAddress Address { get; set; }
效果如图:
16. 实现弹出式编辑对话框,只要将UserControl改成Form,EditStyle改成Modal,service.DropDownControl改成service.ShowDialog
public partial class MyEditorForm : Form { private MyAddress _address; public MyEditorForm(MyAddress address) { InitializeComponent(); _address = address; comboBoxProvince.Text = _address.Province; comboBoxCity.Text = _address.City; } private void comboBoxProvince_SelectedIndexChanged(object sender, EventArgs e) { _address.Province = comboBoxProvince.Text; } private void comboBoxCity_SelectedIndexChanged(object sender, EventArgs e) { _address.City = comboBoxCity.Text; } } public class MyAddressEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; if (service != null) { service.ShowDialog(new MyEditorForm(value as MyAddress)); } return value; } }
17. 密码表示:[PasswordPropertyText(true)]
18. 动态显示/隐藏属性
class MyData { public static void SetPropertyAttribute(object obj, string propertyName, Type attrType, string attrField, object value) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj); Attribute attr = props[propertyName].Attributes[attrType]; FieldInfo field = attrType.GetField(attrField, BindingFlags.Instance | BindingFlags.NonPublic); field.SetValue(attr, value); } private bool _ShowPassword = false; public bool ShowPassword { get { return _ShowPassword; } set { _ShowPassword = value; SetPropertyAttribute(this, "Password", typeof(BrowsableAttribute), "browsable", _ShowPassword); } } [PasswordPropertyText(true)] [Browsable(true)] public string Password { get; set; } } public partial class MainFrm : Form { // 不添加PropertyValueChanged事件,不能实现动态显示/隐藏 private void myData_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) { this.myData.SelectedObject = this.myData.SelectedObject; } }
19. 提供下拉选项的string
public class ListStringConverter : StringConverter { public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { return new StandardValuesCollection(new string[] { "A", "B" }); } public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return false; } } [TypeConverter(typeof(ListStringConverter))] public string Name { get; set; }