PropertyGrid控件(4) 自定义

概要

1. 自定义PropertyGrid 的显示

比如使用 HelpBackColor 设置背景颜色,使用 HelpForeColor 设置字体颜色,用HelpVisible 设置说明 (description) 是否可见等等。

2.如何使用属性 (Attributes)设置属性(Properties) 的显示

  • DescriptionAttribute 说明
  • CategoryAttribute 类别
  • BrowsableAttribute  属性 (Property)是否可见
  • ReadOnlyAttribute 属性 (Property)是否只读
  • DefaultValueAttribute  属性 (Property)缺省值
  • DefaultPropertyAttribute 缺省属性

Customizing the PropertyGrid Control

You can modify some visual aspects of the PropertyGrid to fit your needs. You might want to change how some properties are displayed, and even choose to not display some properties. How customizable is the PropertyGrid?

Changing Visual Aspects of the PropertyGrid

Many visual aspects of the PropertyGrid are customizable. Here is a partial list:

  • Change the background color, change the font color, or hide the description pane through the HelpBackColor, HelpForeColor, and HelpVisible properties.
  • Hide the toolbar through the ToolbarVisible property, change its color through the BackColor property, and display large toolbar buttons through the LargeButtons property.
  • Sort the properties alphabetically, and categorized them using the PropertySort property.
  • Change the splitter color through the BackColor property.
  • Change the grid line and borders through the LineColor property.

For the options window in this example the toolbar is not needed, so set ToolbarVisible to false. Keep the other default settings.

Changing How Properties Are Displayed

To change how some properties are displayed, you can apply different attributes to the properties. Attributes are declarative tags used to annotate programming elements such as types, fields, methods, and properties that can be retrieved at run time using reflection. Here is a partial list:

  • DescriptionAttribute. Sets the text for the property that is displayed in the description help pane below the properties. This is a useful way to provide help text for the active property (the property that has focus). Apply this attribute to the MaxRepeatRate property.
  • CategoryAttribute. Sets the category that the property is under in the grid. This is useful when you want a property grouped by a category name. If a property does not have a category specified, then it will be assigned to the Misc category. Apply this attribute to all properties.
  • BrowsableAttribute - Indicates whether the property is shown in the grid. This is useful when you want to hide a property from the grid. By default, a public property is always shown in the grid. Apply this attribute to the SettingsChanged property.
  • ReadOnlyAttribute - Indicates whether the property is read-only. This is useful when you want to keep a property from being editable in the grid. By default, a public property with get and set accessor functions is editable in the grid. Apply this attribute to the AppVersion property.
  • DefaultValueAttribute - Identifies the property's default value. This is useful when you want to provide a default value for a property and later determine if the property's value is different than the default. Apply this attribute to all properties.
  • DefaultPropertyAttribute - Identifies the default property for the class. The default property for a class gets the focus first when the class is selected in the grid. Apply this attribute to the AppSettings class.

Now apply some of these attributes to the AppSettings class to change the way the properties are displayed in the PropertyGrid.

<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" <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("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

With these attributes applied to theAppSettings class, compile and run the application. The following screen shot shows how it should look.

propertygrid attributes

Figure 3. Properties displayed with categories and default values in the PropertyGrid

After working with this version of the options window, you might notice the following things:

  • The SaveOnClose property gets focus when the window is displayed.
  • The MaxRepeatRate property displays "The rate in milliseconds that the text will repeat" in the description help pane when selected.
  • The SaveOnClose property is displayed under the "Document Settings" category. The other properties are displayed under two other categories named "Global Settings" and "Version."
  • The SettingsChanged property is no longer displayed.
  • The AppVersion property is read-only. Read-only properties are displayed with dimmed text.
  • When the SaveOnClose property has a value other than true, it is displayed in bold text. The PropertyGrid uses bold text to indicate properties that have a non-default value.

 


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

上一页: PropertyGrid控件(3) 使用 返回上级目录: IT知识文章 下一页: PropertyGrid控件(5) 现成属性类型


© 2008 woyouxian.net 版权所有 Contact Us