PropertyGrid控制項下拉式清單

來源:互聯網
上載者:User

 把別人的例子稍做了一下修改,部分內容沒看懂,但程式可以運行,如果有什麼錯誤或不當之處,請指教!

 

 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 }

顯示效果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.