惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
V2EX
博客园 - 【当耐特】
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
月光博客
月光博客
博客园_首页
G
Google Developers Blog
Recent Announcements
Recent Announcements
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - Franky
有赞技术团队
有赞技术团队
I
InfoQ
小众软件
小众软件
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
H
Help Net Security
The Register - Security
The Register - Security
U
Unit 42
P
Palo Alto Networks Blog
N
News and Events Feed by Topic
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CERT Recently Published Vulnerability Notes
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Security Latest
Security Latest
Scott Helme
Scott Helme
罗磊的独立博客
B
Blog RSS Feed
C
Check Point Blog
H
Heimdal Security Blog
T
Threatpost
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Security Affairs
S
Schneier on Security
Apple Machine Learning Research
Apple Machine Learning Research
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
D
DataBreaches.Net

博客园 - jeff377

使用 Sandcastle Help File Builder 制作 VS.NET 的 HELP 文件 [ASP.NET 控件实作 Day31] TBContextMenu 控件三种不同模式的 Click 动作 [ASP.NET 控件实作 Day30] 整合 jQuery ContextMenu plugin 的右键选单控件 [ASP.NET 控件实作 Day29] 解决 DropDownList 成员 Value 值相同产生的问题 [ASP.NET 控件实作 Day28] 图形验证码控件 [ASP.NET 控件实作 Day27] 控件依 FormView CurrentMode 自行设定状态 [ASP.NET 控件实作 Day26] 让你的 GridView 与众不同 [ASP.NET 控件实作 Day25] 自订 GridView 字段 - 日期字段 [ASP.NET 控件实作 Day24] TBDropDownField 的 Items 属性的数据系结 [ASP.NET 控件实作 Day23] 自订 GridVie 字段类别 - 实作 TBDropDownField 字段类别 [ASP.NET 控件实作 Day22] 让 DropDownList 不再因项目清单不存在而造成错误 [ASP.NET 控件实作 Day21] 实作控件智能卷标 [ASP.NET 控件实作 Day20] 侦错设计阶段的程序代码 [ASP.NET 控件实作 Day19] 控件设计阶段的外观 [ASP.NET 控件实作 Day17] 集合属性包含不同型别的成员 [ASP.NET 控件实作 Day16] 继承 WebControl 实作 Toolbar 控件 [ASP.NET 控件实作 Day15] 复合控件隐藏的问题 [ASP.NET 控件实作 Day14] 继承 CompositeControl 实作 Toolbar 控件 [ASP.NET 控件实作 Day13] Flash 控件
[ASP.NET 控件实作 Day18] 修改集合属性编辑器
jeff377 · 2008-10-19 · via 博客园 - jeff377

上篇我们实作了「集合属性包含不同型别的成员」,不过若有去使用属性窗口编辑 TBToolbar 的 Items 属性,你会发觉这个集合属性编辑器无法加入我们定义不同型别的成员,只能加入最原始的集合成员。是不是只能在 aspx 程序代码中手动去输入呢?当然不需要这样人工操作,只要改掉集合属性编辑器就可以达到我们的需求,本文将介绍修改集合属性编辑器的相关作法。

程序代码下载: ASP.NET Server Control - Day18.rar

一、自订集合属性编辑器

我们先看一下 TBToolbar.Items 属性套用的 EditorAttribute,它是使用 CollectionEditor 类别来当作属性编辑器,所以我们就是要继承 CollectionEditor 类别下来修改成自订的属性编辑器。

< _
Editor(GetType(CollectionEditor), GetType(UITypeEditor)) _
> _
Public ReadOnly Property Items() As TBToolbarItemCollection

新增一个继承 CollectionEditor 的 TBToolbarItemCollectionEditor 类别,并加入建构函式。此类别属于 Bee.WebControls.Design 命名空间,通常我们会把设计阶段使用的类别归类到特别的命名空间便于管理及使用。

Namespace WebControls.Design
    Public Class TBToolbarItemCollectionEditor
        Inherits CollectionEditor
 
        ''' <summary>
        ''' 建構函式。
        ''' </summary>
        ''' <param name="Type">型別。</param>
        Public Sub New(ByVal Type As Type)
            MyBase.New(Type)
        End Sub
 
    End Class
End Namespace

我们可以先修改 Items 属性的 EditorAttribute,看看我们自订的 TBToolbarItemCollectionEditor 是否能正常运作。不过这个属性编辑器跟原本的没什么差异,因为我们只是单纯继承下来没做任何异动,接下去我们就要开始来修改这个属性编辑器。

< _
Editor(GetType(TBToolbarItemCollectionEditor), GetType(UITypeEditor)) _
> _
Public ReadOnly Property Items() As TBToolbarItemCollection

二、加入不同型别的集合成员

再来我们就要着手修改集合属性编辑器,让它可以加入不同型别的集合成员。覆写 CollectionEditor 的 CanSelectMultipleInstances 方法传回 True,这个方法是设定 CollectionEditor 是否允许加入多种不同型别的集合成员。

        Protected Overrides Function CanSelectMultipleInstances() As Boolean
            Return True
        End Function

再来覆写 CreateNewItemTypes 方法,这个方法是取得这个集合编辑器可包含的数据型别,将集合可包含的数据型别以数组传回。

        ''' <summary>
        ''' 取得這個集合編輯器可包含的資料型別。
        ''' </summary>
        ''' <returns>這個集合可包含的資料型別陣列。</returns>
        Protected Overrides Function CreateNewItemTypes() As System.Type()
            Dim ItemTypes(2) As System.Type
            ItemTypes(0) = GetType(TBToolbarButton)
            ItemTypes(1) = GetType(TBToolbarTextbox)
            ItemTypes(2) = GetType(TBToolbarLabel)
            Return ItemTypes
        End Function

重建控件组件,使用 Items 的集合属性编辑器,就可以发现「加入」钮的下拉列表就会出现我们所定义的三种型别的集合成员,如此可以加入不同型别的成员了。

 image

三、设定清单项目的显示文字

在成员清单项目中预设会显示成员含命名空间的型别,若我们要修改成比较有识别的显示文字,例如 TBToolbarButton(Key=Add) 可以显示「按钮-Add」,这时可以覆写 GetDisplayText 方法来设定清单项目的显示文字。

        ''' <summary>
        ''' 取出指定清單項目的顯示文字。
        ''' </summary>
        Protected Overrides Function GetDisplayText(ByVal value As Object) As String
            If TypeOf value Is TBToolbarButton Then
                Return String.Format("按鈕 - {0}", CType(value, TBToolbarButton).Key)
            ElseIf TypeOf value Is TBToolbarTextbox Then
                Return "文字框"
            ElseIf TypeOf value Is TBToolbarLabel Then
                Return String.Format("標籤 - {0}", CType(value, TBToolbarLabel).Text)
            Else
                Return value.GetType.Name
            End If
        End Function

image

四、集合编辑器的属性窗口的属性描述

一般属性窗口下面都会有属性描述,可以集合属性编辑器中的属性窗口下面竟没有属性描述。若我们要让它的属性描述可以显示,可以覆写 CreateCollectionForm 方法,取得集合属性编辑窗体,再去设定窗体上的 PropertyGrid.HelpVisible = True 即可。

        ''' <summary>
        ''' 建立集合屬性編輯器表單。
        ''' </summary>
        Protected Overrides Function CreateCollectionForm() As System.ComponentModel.Design.CollectionEditor.CollectionForm
            Dim oForm As CollectionEditor.CollectionForm
            Dim oType As Type
            Dim oFieldInfo As FieldInfo
            Dim oPropertyGrid As System.Windows.Forms.PropertyGrid
 
            oForm = MyBase.CreateCollectionForm()
            oType = oForm.GetType()
            oFieldInfo = oType.GetField("propertyBrowser", BindingFlags.NonPublic Or BindingFlags.Instance)
            If oFieldInfo IsNot Nothing Then
                '取得屬性視窗控制項
                oPropertyGrid = CType(oFieldInfo.GetValue(oForm), System.Windows.Forms.PropertyGrid)
                '設定屬性視窗控制項的[說明]區域為可視
                oPropertyGrid.HelpVisible = True
            End If
            Return oForm
        End Function

image

备注:本文同步发布于「第一届iT邦帮忙铁人赛」,如果你觉得这篇文章对您有帮助,记得连上去推鉴此文增加人气 ^^
http://ithelp.ithome.com.tw/question/10012636