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 }

显示效果如下:

转载于:https://www.cnblogs.com/2008freestyle/archive/2010/04/30/1724910.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值