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 自定义属性的更多相关文章

  1. PropertyGrid控件由浅入深(二):基础用法

    目录 PropertyGrid控件由浅入深(一):文章大纲 PropertyGrid控件由浅入深(二):基础用法 控件的外观构成 控件的外观构成如下图所示: PropertyGrid控件包含以下几个要 ...

  2. PropertyGrid控件由浅入深(一):文章大纲

    Winform中PropertyGrid控件是一个非常好用的对象属性编辑工具,对于Key-Value形式的数据的处理也是非常的好用. 因为Property控件设计良好,在很小的空间内可以展示很多的内容 ...

  3. C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

  4. WinForm窗体PropertyGrid控件的使用

    使用过 Microsoft Visual Basic 或 Microsoft Visual Studio .NET的朋友,一定使用过属性浏览器来浏览.查看或编辑一个或多个对象的属性..NET 框架 P ...

  5. PropertyGrid排序

    解决PropertyGrid对自定义属性排序的问题. 参考了:http://www.cnblogs.com/greatverve/archive/2012/02/08/propergrid-order ...

  6. PropertyGrid—添加属性Tab

    零.引言 PropertyGrid用来显示和编辑对象的属性,前面已经简单介绍了如何使用该控件和提供不同的属性编辑方法.前面主要讲如何使用该控件,但有时,该控件无法满足我们的需求,就需要对其进行扩展.本 ...

  7. c# propertyGrid下拉选项

    实现下面效果的propertygrid属性下拉选择

  8. System.Windows.Forms.PropertyGrid的使用

    PropertyGrid 控件简介 .NET 框架 PropertyGrid 控件是 Visual Studio .NET 属性浏览器的核心.PropertyGrid 控件显示对象或类型的属性,并主要 ...

  9. 数据网格和树-EasyUI Datagrid 数据网格、EasyUI Propertygrid 属性网格、EasyUI Tree 树、EasyUI Treegrid 树形网格

    EasyUI Datagrid 数据网格 扩展自 $.fn.panel.defaults.通过 $.fn.datagrid.defaults 重写默认的 defaults. 数据网格(datagrid ...

随机推荐

  1. what is docker

    尽管之前久闻Docker的大名了,但是天资愚钝,对其到底是个啥东西一直摸不清,最近花了一段时间整理了一下,算是整理出一点头绪来. 官网的介绍是这样的: Docker is an open platfo ...

  2. C#分屏控件用法实例

    本文实例中的自定义类PictureBox继承于UserControl,最终实现简单的分屏功能.分享给大家供大家参考之用.具体实现代码如下: public partial class PictureCo ...

  3. [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式

    1.利用apache提供的commons-httpclient-3.0.jar包 代码如下: /** * 利用HttpClient发起POST请求,并接收返回的响应内容 * * @param url ...

  4. PHP常用文件函数和目录函数整理

    一.常用文件函数库 1.basename(); -- 返回路径中的文件名部分. string basename ( string $path [, string $suffix ] ) //给出一个包 ...

  5. 安装 vsftpd

    (1)安装vsftpdsudo apt-get install vsftpd (2)配置sudo vi /etc/vsftpd.conf #anonymous_enable=YESlocal_enab ...

  6. springMVC+hibernate构建项目

    这几天要用到springMVC+spring+hibernate构建框架,使用的是eclipse今天闲下来把这些记录下来 首先要导入spring 的jar包和hibernate的jar包

  7. Scrapy使用心得

    今天安装了大名鼎鼎的Scrapy,不过碰到了不少问题,包括"Unable to find vcvarsall.bat" 这个错误应该很常见的,用了最简单的解决方法: 安装visua ...

  8. [Excel操作]Microsoft Office Excel 不能访问文件

    最近,客户服务器迁移,因操作系统环境变化而引起的的环境问题一堆,遇到的问题并解决方法在“[Excel]操作”类别会体现. Microsoft Office Excel 不能访问文件“C:\\LMSEx ...

  9. 【转】https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题

    正需要这个,写的很好,就转过来了 转自: http://www.cnblogs.com/naniannayue/ 一:什么是https SSL(Security   Socket   Layer)全称 ...

  10. WPF视频教程系列笔记

    视频二:XAML基础 1.顶级元素 <Window></Window>,<Page></Page>,<Application></Ap ...