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. Centos7升级gcc学习笔记

    概述 最近在学习<深入应用C++11-代码与优化与工程级应用>,我的gcc版本是gcc-4.8.5是支持C++11的,但是我在作者的github上看了一些C++例子,其中有些是C++14的 ...

  2. Cyclic Nacklace

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  3. NLP自然语言处理学习笔记二(初试)

    前言: 用Python对自然语言处理有很好的库.它叫NLTK.下面就是对NLTK的第一尝试. 安装: 1.安装Pip 比较简单,得益于CentOS7自带的easy_install.执行一行命令就可以搞 ...

  4. web 项目 布在tomcat服务器上出现的问题小记

    1.mysql  安装前需要安装.net framework 框架 mysql  无法安装  最后一布,start server 服务起不来. 原因,为上一次mysql没有删除,干净,导入无法安装. ...

  5. JSF HelloWord

    JSF(Java Server Faces)是一种用于构建Web应用程序的新标准Java框架.提供了一种以组件为中心来开发Java Web的用户界面的方法,从而简化了开发.   JSF是Java We ...

  6. 【Android 界面效果36】Fragment管理

    要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager(). 你可以用FragmentManager来做以上事情: ...

  7. 一个Java线程死锁的例子

    package com.lk.B; public class Test4 { private static final Object o1 = new Object(); private static ...

  8. 精通CSS高级Web标准解决方案(1-2 层叠与特殊性)

     层叠与特殊性 选择器的特殊性分成四个等级,a.b.c . d 如果样式是行内样式,那么a=1 b=ID选择器的总数 c=类.伪类.属性选择器的总数 d=标签选择器与伪元素选择器数量 例如:style ...

  9. jquery.validate.js实例演示

    validate是前端重要的交互手段,提升性能的同时更能提升用户操作体验,validate的实现大概有三种方式:HTML5部分支 持,validate验证框架,手动写js或ajax调用接口.使用val ...

  10. 【AR】Vuforia App key is missing.Please get a valid key

    在跑Vuforia 的sample android app 的时候报了下面这个错,找了半天才找到解决方法: "Vuforia App key is missing. Please get a ...