C#实现ProperTyGrid自定义属性的方法
程序员文章站
2023-12-16 16:54:58
本文实例讲解了c#实现propertygrid自定义属性的方法,分享给大家供大家参考。具体方法如下:
一般来说,c#如果要实现自定义属性必须要需要实现接口icustomt...
本文实例讲解了c#实现propertygrid自定义属性的方法,分享给大家供大家参考。具体方法如下:
一般来说,c#如果要实现自定义属性必须要需要实现接口icustomtypedescriptor,具体实现方法如下:
// 摘要: // 提供为对象提供动态自定义类型信息的接口。 public interface icustomtypedescriptor
示例如下:
/// <summary> /// 自定义属性对象 /// </summary> public class myattr { private string name = string.empty; public string name { get { return name; } set { name = value; } } private object value = null; public object value { get { return this.value; } set { this.value = value; } } private string description = string.empty; public string description { get { return description; } set { description = value; } } public override string tostring() { return string.format("name:{0},value:{1}",name.tostring(),value.tostring()); } } /// <summary> /// 自定义性质描述类 /// </summary> public class mypropertydescription : propertydescriptor { private myattr myattr = null; public mypropertydescription(myattr myattr, attribute[] attrs): base(myattr.name, attrs) { this.myattr = myattr; } public override bool canresetvalue(object component) { return false; } public override type componenttype { get { return this.gettype(); } } public override object getvalue(object component) { return myattr.value; } public override bool isreadonly { get { return false; } } public override type propertytype { get { return myattr.value.gettype(); } } public override void resetvalue(object component) { //不重置,无动作 } public override void setvalue(object component, object value) { myattr.value = value; } /// <summary> /// 是否应该持久化保存 /// </summary> /// <param name="component"></param> /// <returns></returns> public override bool shouldserializevalue(object component) { return false; } /// <summary> /// 属性说明 /// </summary> public override string description { get { return myattr.description; } } } /// <summary> /// 实现自定义的特殊属性对象必须继承icustomtypedescriptor,并实现dictionary /// </summary> public class myattrcollection : dictionary<string, myattr>, icustomtypedescriptor { /// <summary> /// 重写add方法 /// </summary> /// <param name="attr"></param> public void add(myattr attr) { if (!this.containskey(attr.name)) { base.add(attr.name, attr); } } public attributecollection getattributes() { return typedescriptor.getattributes(this, true); } public string getclassname() { return typedescriptor.getclassname(this,true); } public string getcomponentname() { return typedescriptor.getclassname(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) { int count=this.values.count; propertydescriptor[] pds=new propertydescriptor[count]; int index = 0; foreach (myattr item in this.values) { pds[index] = new mypropertydescription(item,attributes); index++; } return new propertydescriptorcollection(pds); } public propertydescriptorcollection getproperties() { return typedescriptor.getproperties(this,true); } public object getpropertyowner(propertydescriptor pd) { return this; } }
前台调用如下图所示:
private void btnaddpropertype_click(object sender, eventargs e) { myattr attr = new myattr(); attr.name = txtname.text.trim(); attr.value = txtvalue.text.trim(); attr.description = txtdescription.text.trim(); mac.add(attr); mygrid.refresh(); } private void button1_click(object sender, eventargs e) { addattrcolor(); addattrimage(); addattremun(); mygrid.refresh(); } private void addattremun() { myattr attr = new myattr(); attr.name = "dock"; attr.value = dockstyle.fill; attr.description = "枚举"; mac.add(attr); } private void addattrimage() { myattr attr = new myattr(); attr.name = "image"; attr.value = new bitmap(400,300); attr.description = "图片"; mac.add(attr); } private void addattrcolor() { myattr attr = new myattr(); attr.name = "color"; attr.value = color.red; attr.description = "颜色"; mac.add(attr); }
运行效果如下图所示:
希望本文所述对大家的c#程序设计有所帮助