1. 自定义PropertyGrid 的显示
比如使用 HelpBackColor 设置背景颜色,使用 HelpForeColor 设置字体颜色,用HelpVisible 设置说明 (description) 是否可见等等。
2.如何使用属性 (Attributes)设置属性(Properties) 的显示
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?
Many visual aspects of the PropertyGrid are customizable. Here is a partial list:
For the options window in this example the toolbar is not needed, so set ToolbarVisible to false. Keep the other default settings.
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:
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.

After working with this version of the options window, you might notice the following things:
上一页: PropertyGrid控件(3) 使用 返回上级目录: IT知识文章 下一页: PropertyGrid控件(5) 现成属性类型
© 2008 woyouxian.net 版权所有 Contact Us