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 = ;
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(,);
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# ProperTyGrid 自定义属性的更多相关文章
- PropertyGrid控件由浅入深(二):基础用法
目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...
- PropertyGrid控件由浅入深(一):文章大纲
Winform中PropertyGrid控件是一个非常好用的对象属性编辑工具,对于Key-Value形式的数据的处理也是非常的好用. 因为Property控件设计良好,在很小的空间内可以展示很多的内容 ...
- C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...
- WinForm窗体PropertyGrid控件的使用
使用过 Microsoft Visual Basic 或 Microsoft Visual Studio .NET的朋友,一定使用过属性浏览器来浏览.查看或编辑一个或多个对象的属性..NET 框架 P ...
- PropertyGrid排序
解决PropertyGrid对自定义属性排序的问题. 参考了:http://www.cnblogs.com/greatverve/archive/2012/02/08/propergrid-order ...
- PropertyGrid—添加属性Tab
零.引言 PropertyGrid用来显示和编辑对象的属性,前面已经简单介绍了如何使用该控件和提供不同的属性编辑方法.前面主要讲如何使用该控件,但有时,该控件无法满足我们的需求,就需要对其进行扩展.本 ...
- c# propertyGrid下拉选项
实现下面效果的propertygrid属性下拉选择
- System.Windows.Forms.PropertyGrid的使用
PropertyGrid 控件简介 .NET 框架 PropertyGrid 控件是 Visual Studio .NET 属性浏览器的核心.PropertyGrid 控件显示对象或类型的属性,并主要 ...
- 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格
EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...
随机推荐
- LeetCode15 3Sum
题意: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 转js contains 方法
function Jcontains(root, el) { if (root.compareDocumentPosition) return root === el || !!(root.compa ...
- c++的输入流基础知识
cin是istream类的对象,它从标准输入设备获取数据,程序中的变量通过流提取符“>>”从流中提取数据.从流中提取数据时通常跳过输入流中的空白符 只有在输入完数据并按回车后,该行数据 ...
- 《Cortex-M0权威指南》之Cortex-M0编程入门
转载请注明来源:cuixiaolei的技术博客 嵌入式系统编程入门 微控制器是如何启动的 为了保存编译号的二进制程序代码,大多数的现代微控制器都会包含片上flash存储器.有些微控制器还可能有一个独立 ...
- easyui window自动居中(修复了iframe弹窗被遮盖问题)
$.extend($.fn.window.defaults, { onOpen:function(left, top){ var iframeWidth = $(this).parent().pare ...
- 宽度的100%和auto的区别
前段时间做项目,发现分不清width设为100%和auto的区别,实在是太水了,就查了点资料,做个总结,有不对的地方欢迎大家指出. width:auto 块级元素默认的宽度值.看一下MDN上的解释:T ...
- Ajax实现聊天
用Ajax发送请求,查询数据库是否有自己的数据,如果有自己的数据,就返回 前端页面 <head> <meta charset="UTF-8"> <ti ...
- Backbone.js学习之Router
官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...
- django 学习-12 Django表单 初步
1.先创建项目和应用 django.admin.py startproject cs cd cs django.admin.py startapp blog 2.vim setti ...
- NSTimer实现读秒、倒计时等周期性操作
self.timerSchedule = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(spin ...