• 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 }

    显示效果如下:

  • 相关阅读:
    python学习之函数的参数
    python学习之文件修改及函数基础作业
    日志模块与 re 模块
    day23
    常用模块(二)
    day 22
    python 常用模块
    软件开发目录规范
    day 20
    python 的模块与包
  • 原文地址:https://www.cnblogs.com/2008freestyle/p/1724910.html
Copyright © 2020-2023  润新知