零.引言

  前面一篇文章介绍了如何在PropertyGrid中添加属性Tab,本文主要介绍如何添加事件选项卡。事件在许多对象中都有,尤其是在控件中,如何让对象的事件在PropertyGrid中显示出来呢,本文将进行简单的说明。

一.回顾添加属性Tab

  在上篇文章中详细的讲解了如何添加属性Tab,这里简单回顾一下:

  1.新建一个Tab类并继承于PropertyTab。

  2.重写TabName和Bitmap属性以及GetProperties方法。

  3.给特定类添加PropertyTab特性或将自定义Tab类添加到PropertyGrid的PropertyTabs集合中。

  三步就可以完成了,添加EventTab与此类似,现在问题的关键在于我们要显示的是事件,而GetProperties方法只能返回属性集合(PropertyDescriptorCollection)。怎么办呢,很简单,我们把事件封装成PropertyDescriptor,再返回就行了。

二. 添加EventTab

  GetProperties方法只能返回属性集合。那我们就把事件封装成PropertyDescriptor。首先定义一个封装事件的类,该类继承于PropertyDescriptor:

  
 1     class EventWrapper : PropertyDescriptor
 2     {
 3         string name; //名字
 4         public EventWrapper(string name)
 5             : base(name, null)
 6         {
 7             this.name = name;
 8         }
 9 
10         public override bool CanResetValue(object component)
11         {
12             return true;
13         }
14 
15         public override string DisplayName
16         {
17             get
18             {
19                 return name;
20             }
21         }
22 
23         public override string Description
24         {
25             get
26             {
27                 return string.Format("{0} description", name);
28             }
29         }
30 
31         public override bool IsReadOnly
32         {
33             get { return false; }
34         }
35 
36         public override string Name
37         {
38             get
39             {
40                 return "fs2_" + name;
41             }
42         }
43 
44         public override Type ComponentType
45         {
46             get { throw new NotImplementedException(); }
47         }
48 
49         public override object GetValue(object component)
50         {
51             //do something
52             return "";
53         }
54 
55         public override Type PropertyType
56         {
57             get { return typeof(string); }
58         }
59 
60         public override void ResetValue(object component)
61         {
62             throw new NotImplementedException();
63         }
64 
65         public override void SetValue(object component, object value)
66         {
67             //do something
68         }
69 
70         public override bool ShouldSerializeValue(object component)
71         {
72             return false;
73         }
74     }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2021-08-09
  • 2021-11-18
猜你喜欢
  • 2021-08-16
  • 2021-08-30
相关资源
相似解决方案