摘要
CollectionEditor 是最基本的集合属性编辑器,如 ListBox 的 Items 属性编辑器(ListItemsCollectionEditor)即为继承 CollectionEditor 而来。如图1所示,为 ListBox 的 Items 属性编辑器,编辑窗口右边的属性窗口预设是没有显示 [说明] 区域的。如果我们要让它如图2所示的 GridView 的 Columns 属性编辑器,在属性窗口下方一样具有 [说明] 区域,要如何处理呢?本文将以 ListBox 的 Items 属性编辑器为例,说明如何让 CollectionEditor 的属性窗口一样可以显示 [说明] 区域。

图1. ListBox 的 Items 属性编辑器

图2. GridView 的 Columns 属性编辑器
改写集合属性编辑器
我们要改写 ListBox 的 Items 属性编辑器,故继承 ListItemsCollectionEditor 下来命名为 TBListItemsCollectionEditor。主要作法是覆写 CreateCollectionForm 方法,找到编辑窗口中的属性编辑器控件(System.Windows.Forms.PropertyGrid),并设定 PropertyGrid.HelpVisible = True 即可。
套用 TBListItemsCollectionEditor 属性编辑器
首先继承 ListBox 下来命名为 TBListBox,再覆写 Items 属性,使用 Editor 重新定义该属性编辑器为 TBListItemsCollectionEditor。
测试结果
在页面上放置设计完成的 TBListBox 控件,在属性窗口中编辑 Items 属性,就可以看到我们重新定义的 TBListItemsCollectionEditor 编辑器出现 [说明] 区域。


CollectionEditor 是最基本的集合属性编辑器,如 ListBox 的 Items 属性编辑器(ListItemsCollectionEditor)即为继承 CollectionEditor 而来。如图1所示,为 ListBox 的 Items 属性编辑器,编辑窗口右边的属性窗口预设是没有显示 [说明] 区域的。如果我们要让它如图2所示的 GridView 的 Columns 属性编辑器,在属性窗口下方一样具有 [说明] 区域,要如何处理呢?本文将以 ListBox 的 Items 属性编辑器为例,说明如何让 CollectionEditor 的属性窗口一样可以显示 [说明] 区域。
图1. ListBox 的 Items 属性编辑器
图2. GridView 的 Columns 属性编辑器
改写集合属性编辑器
我们要改写 ListBox 的 Items 属性编辑器,故继承 ListItemsCollectionEditor 下来命名为 TBListItemsCollectionEditor。主要作法是覆写 CreateCollectionForm 方法,找到编辑窗口中的属性编辑器控件(System.Windows.Forms.PropertyGrid),并设定 PropertyGrid.HelpVisible = True 即可。
1
Imports System.Drawing.Design
2
Imports System.Windows.Forms.Design
3
Imports System.ComponentModel
4
Imports System.ComponentModel.Design
5
Imports System.Reflection
6
Imports System.Web.UI.Design.WebControls
7
8
Namespace WebControls.Design
9
Public Class TBListItemsCollectionEditor
10
Inherits ListItemsCollectionEditor
11
12
Public Sub New(ByVal newType As Type)
13
MyBase.new(newType)
14
End Sub
15
16
''' <summary>
17
''' 覆写。建立集合属性编辑器窗体。
18
''' </summary>
19
Protected Overrides Function CreateCollectionForm() As System.ComponentModel.Design.CollectionEditor.CollectionForm
20
Dim oForm As CollectionEditor.CollectionForm
21
Dim oType As Type
22
Dim oFieldInfo As FieldInfo
23
Dim oPropertyGrid As System.Windows.Forms.PropertyGrid
24
25
oForm = MyBase.CreateCollectionForm()
26
oType = oForm.GetType()
27
oFieldInfo = oType.GetField("propertyBrowser", BindingFlags.NonPublic Or BindingFlags.Instance)
28
If oFieldInfo IsNot Nothing Then
29
'取得属性窗口控件
30
oPropertyGrid = CType(oFieldInfo.GetValue(oForm), System.Windows.Forms.PropertyGrid)
31
'设定属性窗口控件的[说明]区域为可视
32
oPropertyGrid.HelpVisible = True
33
End If
34
Return oForm
35
End Function
36
37
End Class
38
End Namespace
Imports System.Drawing.Design2
Imports System.Windows.Forms.Design3
Imports System.ComponentModel4
Imports System.ComponentModel.Design5
Imports System.Reflection6
Imports System.Web.UI.Design.WebControls7

8
Namespace WebControls.Design9
Public Class TBListItemsCollectionEditor10
Inherits ListItemsCollectionEditor11

12
Public Sub New(ByVal newType As Type)13
MyBase.new(newType)14
End Sub15

16
''' <summary>17
''' 覆写。建立集合属性编辑器窗体。18
''' </summary>19
Protected Overrides Function CreateCollectionForm() As System.ComponentModel.Design.CollectionEditor.CollectionForm20
Dim oForm As CollectionEditor.CollectionForm21
Dim oType As Type22
Dim oFieldInfo As FieldInfo23
Dim oPropertyGrid As System.Windows.Forms.PropertyGrid24

25
oForm = MyBase.CreateCollectionForm()26
oType = oForm.GetType()27
oFieldInfo = oType.GetField("propertyBrowser", BindingFlags.NonPublic Or BindingFlags.Instance)28
If oFieldInfo IsNot Nothing Then29
'取得属性窗口控件30
oPropertyGrid = CType(oFieldInfo.GetValue(oForm), System.Windows.Forms.PropertyGrid)31
'设定属性窗口控件的[说明]区域为可视32
oPropertyGrid.HelpVisible = True33
End If34
Return oForm35
End Function36

37
End Class38
End Namespace套用 TBListItemsCollectionEditor 属性编辑器
首先继承 ListBox 下来命名为 TBListBox,再覆写 Items 属性,使用 Editor 重新定义该属性编辑器为 TBListItemsCollectionEditor。
1
Imports System
2
Imports System.Collections.Generic
3
Imports System.ComponentModel
4
Imports System.Text
5
Imports System.Web
6
Imports System.Web.UI
7
Imports System.Web.UI.WebControls
8
Imports Bee.Web.WebControls.Design
9
10
Namespace WebControls
11
< _
12
Description("清单控件"), _
13
ToolboxData("<{0}:TBListBox runat=server></{0}:TBListBox>") _
14
> _
15
Public Class TBListBox
16
Inherits ListBox
17
18
<Editor(GetType(TBListItemsCollectionEditor), GetType(System.Drawing.Design.UITypeEditor))> _
19
Public Overrides ReadOnly Property Items() As ListItemCollection
20
Get
21
Return MyBase.Items
22
End Get
23
End Property
24
25
End Class
26
End Namespace
Imports System2
Imports System.Collections.Generic3
Imports System.ComponentModel4
Imports System.Text5
Imports System.Web6
Imports System.Web.UI7
Imports System.Web.UI.WebControls8
Imports Bee.Web.WebControls.Design9

10
Namespace WebControls11
< _12
Description("清单控件"), _13
ToolboxData("<{0}:TBListBox runat=server></{0}:TBListBox>") _14
> _15
Public Class TBListBox16
Inherits ListBox17

18
<Editor(GetType(TBListItemsCollectionEditor), GetType(System.Drawing.Design.UITypeEditor))> _19
Public Overrides ReadOnly Property Items() As ListItemCollection20
Get21
Return MyBase.Items22
End Get23
End Property24

25
End Class26
End Namespace测试结果
在页面上放置设计完成的 TBListBox 控件,在属性窗口中编辑 Items 属性,就可以看到我们重新定义的 TBListItemsCollectionEditor 编辑器出现 [说明] 区域。

浙公网安备 33010602011771号