PropertyGrid控件(5) 现成属性类型

概要:可以使用.NET提供现成的类型 ,Size type, Font type, Color type 等作为 PropertyGrid 的属性类型。

Displaying Complex Properties

So far, the options window has displayed simple types like integers, Booleans, and strings. What about more complex types? What if your application needs to track something like window size, document font, or toolbar color? Some data types provided by the .NET Framework have special display abilities that help make them more usable in the PropertyGrid.
Support for Provided Types

First, update the AppSettings class to add new properties for window size (Size type), window font (Font type), and toolbar color (Color type).

<DefaultPropertyAttribute("SaveOnClose")> _
Public Class AppSettings
Private _saveOnClose As Boolean = True
Private _greetingText As String = "Welcome to your application!"
Private _maxRepeatRate As Integer = 10
Private _itemsInMRU As Integer = 4 Private _settingsChanged As Boolean = False
Private _appVersion As String = "1.0" Private _windowSize As Size = New Size(100, 100)
Private _windowFont As Font = New Font("Arial", 8, FontStyle.Regular)
Private _toolbarColor As Color = SystemColors.Control <CategoryAttribute("Document Settings"), _
DefaultValueAttribute(True)> _
Public Property SaveOnClose() As Boolean
Get
Return _saveOnClose
End Get
Set(ByVal Value As Boolean)
SaveOnClose = Value
End Set
End Property <CategoryAttribute("Document Settings")> _
Public Property WindowSize() As Size
Get
Return _windowSize
End Get
Set(ByVal Value As Size)
_windowSize = Value
End Set
End Property <CategoryAttribute("Document Settings")> _
Public Property WindowFont() As Font
Get
Return _windowFont
End Get
Set(ByVal Value As Font)
_windowFont = Value
End Set
End Property <CategoryAttribute("Global Settings")> _
Public Property ToolbarColor() As Color
Get
Return _toolbarColor
End Get
Set(ByVal Value As Color)
_toolbarColor = Value
End Set
End Property <CategoryAttribute("Global Settings"), _
ReadOnlyAttribute(True), _
DefaultValueAttribute("Welcome to your application!")> _
Public Property GreetingText() As String
Get
Return _greetingText
End Get
Set(ByVal Value As String)
_greetingText = Value
End Set
End Property <CategoryAttribute("Global Settings"), _
DefaultValueAttribute(4)> _
Public Property ItemsInMRUList() As Integer
Get
Return _itemsInMRU
End Get
Set(ByVal Value As Integer)
_itemsInMRU = Value
End Set
End Property <DescriptionAttribute("The rate in milliseconds that the text will repeat."), _
CategoryAttribute("Global Settings"), _
DefaultValueAttribute(10)> _
Public Property MaxRepeatRate() As Integer
Get
Return _maxRepeatRate
End Get
Set(ByVal Value As Integer)
_maxRepeatRate = Value
End Set
End Property <BrowsableAttribute(False),
DefaultValueAttribute(False)> _
Public Property SettingsChanged() As Boolean
Get
Return _settingsChanged
End Get
Set(ByVal Value As Boolean)
_settingsChanged = Value
End Set
End Property <CategoryAttribute("Version"), _
DefaultValueAttribute("1.0"), _
ReadOnlyAttribute(True)> _
Public Property AppVersion() As String
Get
Return _appVersion
End Get
Set(ByVal Value As String)
_appVersion = Value
End Set
End Property
End Class

The following screen shot shows how the new properties look in the PropertyGrid.

propertygrid, font,size,color type,

Figure 4. .NET Framework data types displayed in the PropertyGrid

Notice that the WindowFont property has an ellipsis ("...") button that displays a font selection dialog when pressed. In addition, the property can be expanded to display more Font properties. Some of the Font properties provide a drop-down list of values and details about the font. The WindowSize property can be expanded to show more properties of the Size type. And finally, notice that the ToolbarColor property has a swatch of the selected color and a custom drop-down list to select different colors. For these data types and others, the .NET Framework provides additional classes to make editing in the PropertyGrid easier.


编者或作者: 我有闲    收录日期: 2008-12-21
参考或来源: 我有闲IT教程网 WoYouXian.net

上一页: PropertyGrid控件(4) 自定义 返回上级目录: IT知识文章 下一页: PropertyGrid控件(6) 自定义属性类型


© 2008 woyouxian.net 版权所有 Contact Us