参考http://www.cnblogs.com/yank/archive/2011/09/17/2179598.html

在上述文档的基础上做了改进。从EnumConverter类派生

显示效果:

1、定义枚举:在枚举中加入描述信息,作为我们需要显示的信息;

enum MediaType
{
[Description("文本")]
Text = ,
[Description("图片")]
Image = ,
[Description("视频")]
Video =
}

2、定义TypeConverter,对枚举进行转换;

class EnumTypeConverter:EnumConverter
{
private Type _enumType; public EnumTypeConverter(Type type): base(type)
{
_enumType = type;
} public override bool CanConvertTo(ITypeDescriptorContext context,Type destType)
{
return destType == typeof(string);
} public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,object value, Type destType)
{
FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, value));
DescriptionAttribute da =(DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)); if (da != null)
return da.Description;
else
return value.ToString();
} public override bool CanConvertFrom(ITypeDescriptorContext context,Type srcType)
{
return srcType == typeof(string);
} public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,object value)
{
foreach (FieldInfo fi in _enumType.GetFields())
{
DescriptionAttribute da =(DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)); if ((da != null) && ((string)value == da.Description))
return Enum.Parse(_enumType, fi.Name);
}
return Enum.Parse(_enumType, (string)value);
}
}

3、属性使用TypeConverter;

class MediaInfo
{ [Category("媒体类型"), DisplayName("名称"), Description("Media Type")]
public string MediaName { get; set; } [DisplayName("媒体类型"), Description("Media Type")]
[Category("媒体类型"),TypeConverter(typeof(EnumTypeConverter))]
public MediaType MediaTypeDisplay { get; set; } public int ID { get; set; }
}

PropertyGrid中的枚举显示为中文的更多相关文章

  1. Winform中实现ZedGraph中曲线右键显示为中文

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  2. tomcat中的URL参数为中文,servlet接收后显示乱码

    URL中参数的值为中文时,servlet接收后显示为乱码,如下图: 这时候需要修改tomcat的中的server.xml文件.该文件路径为 tomcat安装目录下的conf文件夹.   为修改前的se ...

  3. 将浏览器地址栏中的 Request 参数显示成中文

    希望实现:在当 JSP 页面发起请求,或者 Servlet 跳转时,地址栏中的参数可以显示成中文. 在通常情况下,浏览器地址栏中的URL地址为了适配不同的浏览器,会将URL地址信息转码为"I ...

  4. 如何让django模型中的字段和model名显示为中文

    如何让django模型中的字段和model名显示为中文:在模型中加入class Meta即可 class People(models.Model): name = models.CharField(n ...

  5. Python将list中的unicode转换成中文显示

    有这样一个列表: list = [{'channel_id': -3, 'name': u'\u7ea2\u5fc3\u5146\u8d6b'}, {u'seq_id': 0, u'name_en': ...

  6. java枚举与.net中的枚举区别

    通过一段时间的项目实践,发现java中的枚举与.net中的枚举有很大的差别,初期造成了我对java中的枚举一些错误理解及部分有缺陷的应用,其实追其原因还是因为我会习惯性的认为java的枚举在作用以及定 ...

  7. JFreechart在linux下不显示及中文乱码问题

    一.使用JFreeChart建的报表,在window下能正常显示,但是放到linux下就报错,而且有时候会把tomcat挂掉, 原因是jfreechart的在linux系统中需要访问java awt库 ...

  8. JQuery中Ajax的Post提交中文乱码的解决方案

    出自:http://m.blog.csdn.net/blog/blueheart20/26370023 引言: 在JQuery的Ajax POST请求中,进行请求,其中的中文在后台,显示为乱码,该如何 ...

  9. C#中的枚举类型(enum type)

    ylbtech 原文 C#中的枚举类型(enum type) 概念 枚举类型(enum type)是具有一组命名常量的独特的值类型.在以下示例中: enum Color { Red, Green, B ...

随机推荐

  1. regex_replace

    Regex_iterator方法需要输入一个正则表达式,以及一个用于替换匹配的字符串的格式化字符串:这个格式化的字符串可以通过表的转义序列引用匹配子字符串的部分内容: 转义序列 $n 替换第n个捕获的 ...

  2. JDK(十)JDK1.7&1.8源码对比分析【集合】ConcurrentHashMap

    前言 在JDK1.7&1.8源码对比分析[集合]HashMap中我们对比分析了JDK1.7和1.8版本的HashMap源码,趁热打铁,这篇文章就来看看JDK1.7和1.8版本的Concurre ...

  3. python apsheduler cron 参数解析

    from:https://apscheduler.readthedocs.io/en/v2.1.2/cronschedule.html Cron-style scheduling This is th ...

  4. centos7 支持中文显示(转)

    centos7 支持中文显示 - kingleoric - 博客园https://www.cnblogs.com/kingleoric/p/7517753.html http://www.linuxi ...

  5. C\C++ vector 构造函数 & 析构函数

    #include <iostream> #include <vector> using namespace std; class Obj { public: Obj(void) ...

  6. 关于java8(Stream)的一些用法

    如果要处理int[] 转换成 List<Integer>这种形式的,可以用下面这个方法: List<Integer> orgIds = Arrays.stream(reqVo. ...

  7. The Gene of Bitizens

    1.          Summary The document is about the general idea of the architecture design of the Bitizen ...

  8. 泰坦尼克(Titanic)生存因素可视化

    数据来源: kaggle 分析工具:Python 3.6 & jupyter notebook 附上数据:链接: https://pan.baidu.com/s/1D7JNvHmqTIw0Oo ...

  9. Linux部署python django程序-apache

    1.安装Apache 先卸载自带的httpd rpm -e httpd --nodeps 在网上下载四个文件 1.apr-1.4.6.tar.gz 2.apr-util-1.5.1.tar.gz 3. ...

  10. 14-HTML-CSS案例

    1.超链接美化 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...