C# PropertyGrid封装类(VS2010)
功能简介:
1、支持下拉、打开文件、文本输入、点击项可显示描述内容。
以下业务代码则是调用封装类。
Category :属性类
DisplayName:名称
Descriptor:描述内容
Editor:编辑器
private PropertyManage _pmc=new PropertyManage();/// <summary>/// 绑定PropertyGrid/// </summary>/// <returns></returns>public Object GetPropertyObject(){ #region 方案配置Property pp = new Property("SchemeName", "test.xml", false, true);pp.Category = "方案配置";pp.DisplayName = "方案";pp.Editor = new PropertyGridFileEditor();_pmc.Add(pp);#endregion#region 表计规格pp = new Property("MeterType", "1S");pp.Category = "表计规格";pp.DisplayName = "表计类型";string[] s = new string[] { "1S", "2S", "3S" };pp.Converter = new DropDownListConverter(s);_pmc.Add(pp);pp = new Property("RatedVolt","220");pp.Category = "表计规格";pp.DisplayName = "额定电压";pp.Descriptor = "V";_pmc.Add(pp);pp = new Property("RatedCurrent", "0");pp.Category = "表计规格";pp.DisplayName = "额定电流";pp.Descriptor = "A";_pmc.Add(pp);pp = new Property("RatedFreq", "60");pp.Category = "表计规格";pp.DisplayName = "额定频率";pp.Descriptor = "Hz";_pmc.Add(pp);pp = new Property("Phase","三相四线");pp.Category = "表计规格";pp.DisplayName = "接线方式";s=new string[]{"三相四线","三相三相","单相"};pp.Converter = new DropDownListConverter(s);_pmc.Add(pp);#endregion return _pmc;}//最后PropertyGrid控件调用
PropertyGrid.SelectedObject = GetPropertyObject();
以下封装代码可直接拷贝,提示错误则是未引用相关命名控件,右键解析自动添加。(PS:修改他人成果)
region 封装操作PropertyGrid属性方法//属性管理类public class PropertyManage : CollectionBase, ICustomTypeDescriptor{public void Add(Property value){int flag = -1;if (value != null){if (base.List.Count > 0){IList<Property> mList = new List<Property>();for (int i = 0; i < base.List.Count; i++){Property p = base.List[i] as Property;if (value.Name == p.Name){flag = i;}mList.Add(p);}if (flag == -1){mList.Add(value);}base.List.Clear();foreach (Property p in mList){base.List.Add(p);}}else{base.List.Add(value);}}}public void Remove(Property value){if (value != null && base.List.Count > 0)base.List.Remove(value);}public Property this[int index]{get{return (Property)base.List[index];}set{base.List[index] = (Property)value;}}#region ICustomTypeDescriptor 成员public AttributeCollection GetAttributes(){return TypeDescriptor.GetAttributes(this, true);}public string GetClassName(){return TypeDescriptor.GetClassName(this, true);}public string GetComponentName(){return TypeDescriptor.GetComponentName(this, true);}public TypeConverter GetConverter(){return TypeDescriptor.GetConverter(this, true);}public EventDescriptor GetDefaultEvent(){return TypeDescriptor.GetDefaultEvent(this, true);}public PropertyDescriptor GetDefaultProperty(){return TypeDescriptor.GetDefaultProperty(this, true);}public object GetEditor(Type editorBaseType){return TypeDescriptor.GetEditor(this, editorBaseType, true);}public EventDescriptorCollection GetEvents(Attribute[] attributes){return TypeDescriptor.GetEvents(this, attributes, true);}public EventDescriptorCollection GetEvents(){return TypeDescriptor.GetEvents(this, true);}public PropertyDescriptorCollection GetProperties(Attribute[] attributes){PropertyDescriptor[] newProps = new PropertyDescriptor[this.Count];for (int i = 0; i < this.Count; i++){Property prop = (Property)this[i];newProps[i] = new CustomPropertyDescriptor(ref prop, attributes);}return new PropertyDescriptorCollection(newProps);}public PropertyDescriptorCollection GetProperties(){return TypeDescriptor.GetProperties(this, true);}public object GetPropertyOwner(PropertyDescriptor pd){return this;}#endregion}//属性类 public class Property{private string _name = string.Empty;private object _value = null;private bool _readonly = false;private bool _visible = true;private string _category = string.Empty;private string _descriptor = string.Empty;TypeConverter _converter = null;object _editor = null;private string _displayname = string.Empty;public Property(string sName, object sValue){this._name = sName;this._value = sValue;}public Property(string sName, object sValue, bool sReadonly, bool sVisible){this._name = sName;this._value = sValue;this._readonly = sReadonly;this._visible = sVisible;}public string Name //获得属性名 {get{return _name;}set{_name = value;}}public string DisplayName //属性显示名称 {get{return _displayname;}set{_displayname = value;}}public TypeConverter Converter //类型转换器,制作下拉列表时需要用到 {get{return _converter;}set{_converter = value;}}public string Category //属性所属类别 {get{return _category;}set{_category = value;}}public object Value //属性值 {get{return _value;}set{_value = value;}}public string Descriptor{get { return _descriptor; }set { _descriptor = value; }}public bool ReadOnly //是否为只读属性 {get{return _readonly;}set{_readonly = value;}}public bool Visible //是否可见 {get{return _visible;}set{_visible = value;}}public virtual object Editor //属性编辑器 {get{ return _editor; }set{_editor = value;}}}//属性描述类public class CustomPropertyDescriptor : PropertyDescriptor{Property m_Property;public CustomPropertyDescriptor(ref Property myProperty, Attribute[] attrs): base(myProperty.Name, attrs){m_Property = myProperty;}#region PropertyDescriptor 重写方法public override bool CanResetValue(object component){return false;}public override Type ComponentType{get{return null;}}public override object GetValue(object component){return m_Property.Value;}public override string Description{get{ return m_Property.Descriptor;}}public override string Category{get{return m_Property.Category;}}public override string DisplayName{get{return m_Property.DisplayName != "" ? m_Property.DisplayName : m_Property.Name;}}public override bool IsReadOnly{get{return m_Property.ReadOnly;}}public override void ResetValue(object component){//Have to implement }public override bool ShouldSerializeValue(object component){return false;}public override void SetValue(object component, object value){m_Property.Value = value;}public override TypeConverter Converter{get{return m_Property.Converter;}}public override Type PropertyType{get { return m_Property.Value.GetType(); }}public override object GetEditor(Type editorBaseType){return m_Property.Editor == null ? base.GetEditor(editorBaseType) : m_Property.Editor;}#endregion}//文件对话框编辑类public class PropertyGridFileEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value) { IWindowsFormsEditorService edSvc=(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); if (edSvc != null) { // 可以打开任何特定的对话框 OpenFileDialog dialog = new OpenFileDialog(); dialog.AddExtension = false; if (dialog.ShowDialog().Equals(DialogResult.OK)) { return dialog.FileName; } } return value; } }//文件下拉转换类public class DropDownListConverter : StringConverter{object[] m_Objects;public DropDownListConverter(object[] objects){m_Objects = objects;}public override bool GetStandardValuesSupported(ITypeDescriptorContext context){return true;}public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return true;}public overrideSystem.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context){return new StandardValuesCollection(m_Objects);//可以直接在内部定义一个数组,但并不建议这样做,这样对于下拉框的灵活 //性有很大影响 }}#endregion
C# PropertyGrid封装类(VS2010)相关推荐
- VS2010 编译 QT4.8.7 x64
1 下载qt4.8.7源代码,解压到合适位置(如本文为d:\qt\4.8.7) 2 设置环境变量: set QMAKESPEC=win32-msvc2010 set QTDIR=d:\qt\4.8.7 ...
- 怎么在vs2010中使用ActiveX Test Container(转)
ActiveX Test Container Application is Still Available(转) Hello, I'm Pat Brenner, a developer on the ...
- VS2010创建ATL类时需要手动填写ProgID
在新建ATL类的时候VS2010默认是不填写ProgID的: 所以默认创建的类生成的rgs文件中只有NoRemove CLSID这一栏,导致在JS中使用new ActivexObject(" ...
- osgearth+vs2010安装
OSGEARTH + VS2010 安装 *VS 平台不重要,本教程也适用于VS2008等.假设我的OSG目录为:D:/OSG *本教程参考网上osgearth+vs2008安装. 一.准备工作 下载 ...
- VS2010非永久性配置和永久配置Opencv
前面的非永久性配置在很多博客中都已经详细说明,这里就大致说一下. 按照非永久性的步骤配置好之后,就可以在此基础上永久性配置. 1.下载安装OpenCV 下载地址如下: http://sourcef ...
- windows下opencv安装及配置(vs2010环境)
opecv下载 前往官方下载地址 https://opencv.org/releases/ opencv安装及配置 解压,会得到一个opencv文件夹,可将提取出的文件移动到任意位置,我将其放在F盘. ...
- 用vs2010打开使用vs2013升级后的WP工程
项目在win7+vs2010的环境中建立的,后来在win8.1+vs2013的环境下修改和完善: 但是所有功能实现后发现wp7项目在使用vs2013打开后因为单向升级的原因,项目只能被编译为wp8项目 ...
- VS2008 VS2010发布网站时如何产生固定命名的 Dll 文件
VS2008 发布网站时如何产生固定命名的 Dll 文件 dev.firnow.com 时间 : 2010-12-08 作者:网络 编辑:fnw 点击: 82 [ 评论 ] - - VS ...
- 使用VS2010调试技巧让C指针无处遁形
Linux 下调试远没有windows下的VS方便,不管是VC++6还是VS2003,2005,2008,2010,2012. VS2012自动格式化代码 Ctrl+K+D VS下调试一定要注意尽量不 ...
最新文章
- CSS3关于过渡效果的问题
- 2020-09-25C++学习笔记引用2:二级指针形参与指针形参的使用方法和区别重难点,主看综合代码(1、指针形参2、引用形参3、二级指针形参4、指针引用形参)
- linux谁动了我的服务器,linux系统监控之谁动了我的服务器?
- python3 字典 dict 常见用法总结(判断key是否存在)
- css 科技 边框_CSS 边框
- 使用字符代替圆角尖角研究(转)
- 前后台交互经常使用的技术汇总(后台:Java技术,前台:Js或者Jquery)
- kettle mysql 导入数据库_ETL工具—Kettle数据的导入导出—数据库到数据库
- oracle11g数据库登录01017,sqlplus登录Oracle时ORA-01017: invalid username/password; logon denied的错误...
- python有什么证可以考1002python有什么证可以考_离python二级考还有十几天,吓的我赶紧买了本python教程...
- PE格式第八讲,TLS表(线程局部存储)
- Ubuntu 16.04使用阿里云邮箱命令行发送邮件配置
- cad画直线长度与实际不符_cad画规定长度直线的方法步骤图
- 爬虫 第二讲 urllib模块和requests模块
- 百度迟到移动互联网:寻入口级产品 文化成挑战
- @Primary注解在spring中的使用
- 智能网联汽车云控系统第3部分:路云数据交互规范
- STM32并口驱动AD9854——HAL库
- 2021年应届生面试题(一文到底)
- python算24点穷举法_24点游戏7节课–第1节-游戏介绍与基本算法 | 学步园