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 ...
随机推荐
- oracle内置sql函数(转)
转自:http://www.cnblogs.com/yening10000/archive/2009/07/02/1515284.html F.1字符函数——返回字符值 这些函数全都接收的是字符族类型 ...
- Frogs' Neighborhood
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 7920 Accepted: 33 ...
- A problem is easy
描述When Teddy was a child , he was always thinking about some simple math problems ,such as “What it’ ...
- [super dealloc]内存释放的先后顺序
心得:从前做内存释放,只是觉得应该,没体会到这个的重要性,如果不及时释放就会有很多内存泄露,就像我早期遇到的前赴后继的崩溃,比如:没使用完,就释放会崩溃等明显的release问题. 作为全局的 ...
- 结合源码看nginx-1.4.0之nginx多进程机制详解
目录 0. 摘要 1. nginx多进程设计思想 2. nginx多进程设计数据结构 3. nginx进程间通信机制 4. 一个简单的多进程例子 5. 小结 6. 参考源码
- java正则表达式常用实例——借鉴思路
转载自:http://mp.weixin.qq.com/s?__biz=MjM5OTM4NDMyMg==&mid=2650044497&idx=1&sn=dc80fa35f7e ...
- extjs中grid对于其中表单的表头的读取以及是否隐藏的判断
//获取grid的表头信息 var columns=baseGrid.columns; alert(columns.length); //判断列是否隐藏并输出表 ...
- 递归删除指定目录下的 .git 文件
转载自:http://my.oschina.net/armsky/blog/34447 find . -name .git | xargs rm -fr 其中对 xargs 的介绍,可以参照以下内容: ...
- struts2 零配置
一.新建一个web项目,命名为:struts2 二.导入strut2所需的jar包 所需jar下载:http://pan.baidu.com/s/1dDxP4Z3 三.配置struts2的启动文件,在 ...
- angularJs中筛选功能-angular.filter-1
技术分享:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angular-filter-learn-1/ 以下介绍为自己在使用ang ...