zoukankan      html  css  js  c++  java
  • PropertyGrid控件下拉列表

     把别人的例子稍做了一下修改,部分内容没看懂,但程序可以运行,如果有什么错误或不当之处,请指教!

    1 /// <summary>
    2 /// 一.自定义一个特性类ListAttribute,提供下拉列表值:
    3 /// </summary>
    4   public class ListAttribute : Attribute
    5 {
    6 public string[] _lst;
    7
    8 public ListAttribute(string[] lst)
    9 {
    10 //初始化列表值
    11   _lst =lst;
    12 }
    13 }
    14
    15 /// <summary>
    16 /// 二.特性转换器MyConverter
    17 /// </summary>
    18   public class MyConverter : ExpandableObjectConverter
    19 {
    20 public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    21 {
    22 return true;
    23 }
    24
    25 public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    26 {
    27 return true;
    28 }
    29
    30 public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    31 {
    32 ListAttribute listAttribute = (ListAttribute)context.PropertyDescriptor.Attributes[typeof(ListAttribute)];
    33 StandardValuesCollection vals = new TypeConverter.StandardValuesCollection(listAttribute._lst);
    34
    35 return vals;
    36 }
    37
    38 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    39 {
    40 return true;
    41 }
    42 }
    43
    44 /// <summary>
    45 /// 三.应用示例:
    46 /// </summary>
    47 public class MyObject
    48 {
    49 private string _name;
    50
    51 [CategoryAttribute("信息"), DescriptionAttribute("姓名"),
    52 TypeConverter(typeof(MyConverter)), ListAttribute( new string[] { "张三", "李四", "王五", "赵六", "马七" })]
    53 public string Name
    54 {
    55 get { return _name; }
    56 set { _name = value; }
    57 }
    58 }
    59
    60 private void Form1_Load(object sender, EventArgs e)
    61 {
    62 this.propertyGrid1.SelectedObject = new MyObject();
    63 }

    显示效果如下:

  • 相关阅读:
    SharePoint 中AJAX请求报错
    SharePoint Online 站点启用内容编辑器部件
    SharePoint 表单开发常用脚本[不断更新ing]
    SharePoint Online 工作流添加历史记录
    BBC评出的100本最具影响力经典书籍
    描写人物的成语汇总,请为孩子收藏!
    失传已久,1917年的满分作文,惊现于世!
    MySQL用户及权限
    数据库SQL优化大总结之 百万级数据库优化方案(转载)
    3分钟弄懂中国金融体系
  • 原文地址:https://www.cnblogs.com/2008freestyle/p/1724910.html
Copyright © 2011-2022 走看看