摘要
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 即可。
 1Imports System.Drawing.Design
 2Imports System.Windows.Forms.Design
 3Imports System.ComponentModel
 4Imports System.ComponentModel.Design
 5Imports System.Reflection
 6Imports System.Web.UI.Design.WebControls
 7
 8Namespace 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

38End Namespace

套用 TBListItemsCollectionEditor 属性编辑器
首先继承 ListBox 下来命名为 TBListBox,再覆写 Items 属性,使用 Editor 重新定义该属性编辑器为 TBListItemsCollectionEditor。
 1Imports System
 2Imports System.Collections.Generic
 3Imports System.ComponentModel
 4Imports System.Text
 5Imports System.Web
 6Imports System.Web.UI
 7Imports System.Web.UI.WebControls
 8Imports Bee.Web.WebControls.Design
 9
10Namespace 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

26End Namespace

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






 

posted on 2008-05-19 20:11  jeff377  阅读(3243)  评论(1编辑  收藏  举报