翻译|其它|编辑:郝浩|2006-03-22 10:00:00.000|阅读 2391 次
概述:
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
摘要:本文旨在帮助您了解 Microsoft .NET 框架中的 PropertyGrid 控件,以及如何针对您的应用程序自定义该控件。
适用于:
Microsoft® .NET® 框架
Microsoft® Visual Studio® .NET
PropertyGrid 控件简介
创建 PropertyGrid 控件
何处使用 PropertyGrid 控件
选择对象
自定义 PropertyGrid 控件
显示复杂属性
为属性提供自定义 UI
小结
如果您使用过 Microsoft® Visual Basic® 或 Microsoft Visual Studio .NET,那么您一定使用过属性浏览器来浏览、查看和编辑一个或多个对象的属性。.NET 框架 PropertyGrid 控件是 Visual Studio .NET 属性浏览器的核心。PropertyGrid 控件显示对象或类型的属性,并主要通过使用反射来检索项目的属性。(反射是在运行时提供类型信息的技术。)
下面的屏幕快照显示了 PropertyGrid 在窗体上的外观。
图 1:窗体上的 PropertyGrid
PropertyGrid 包含以下部分:
要使用 Visual Studio .NET 创建 PropertyGrid 控件,需要将该控件添加到工具箱中,因为默认情况下并不包含该控件。在 Tools(工具)菜单中,选择 Customize Toolbox(自定义工具箱)。在对话框中选择 Framework Components(框架组件)选项卡,然后选择 PropertyGrid。
如果您从命令行编译代码,请使用 /reference 选项并指定 System.Windows.Forms.dll。
以下代码显示了如何创建 PropertyGrid 控件并将其添加到窗体中。
' Visual Basic Imports System
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Globalization
Public Class OptionsDialog
Inherits System.Windows.Forms.Form
Private OptionsPropertyGrid As System.Windows.Forms.PropertyGrid
Public Sub New()
MyBase.New()
OptionsPropertyGrid = New PropertyGrid()
OptionsPropertyGrid.Size = New Size(300, 250)
Me.Controls.Add(OptionsPropertyGrid)
Me.Text = "选项对话框"
End Sub
End Class //C# using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Globalization;
public class OptionsDialog : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid OptionsPropertyGrid;
public OptionsDialog()
{
OptionsPropertyGrid = new PropertyGrid();
OptionsPropertyGrid.Size = new Size(300, 250);
this.Controls.Add(OptionsPropertyGrid);
this.Text = "选项对话框";
}
[STAThread]
static void Main()
{
Application.Run(new OptionsDialog());
}
}
在应用程序中的很多地方,您都可以使用户与 PropertyGrid 进行交互,从而获得更丰富的编辑体验。例如,某个应用程序包含多个用户可以设置的“设置”或选项,其中一些可能十分复杂。您可以使用单选按钮、组合框或文本框来表示这些选项。但本文将逐步介绍如何使用 PropertyGrid 控件创建选项窗口来设置应用程序选项。上面所创建的 OptionsDialog
窗体即是选项窗口的开始。现在,我们创建一个名为 AppSettings
的类,其中包含映射到应用程序设置的所有属性。如果创建单独的类而不使用多个分散的变量,设置将更便于管理和维护。
' Visual Basic Public Class AppSettings
Private _saveOnClose As Boolean = True
Private _greetingText As String = "欢迎使用应用程序!"
Private _maxRepeatRate As Integer = 10
Private _itemsInMRU As Integer = 4
Private _settingsChanged As Boolean = False
Private _appVersion As String = "1.0"
Public Property SaveOnClose() As Boolean
Get
Return _saveOnClose
End Get
Set(ByVal Value As Boolean)
SaveOnClose = Value
End Set
End Property
Public Property GreetingText() As String
Get
Return _greetingText
End Get
Set(ByVal Value As String)
_greetingText = Value
End Set
End Property
Public Property ItemsInMRUList() As Integer
Get
Return _itemsInMRU
End Get
Set(ByVal Value As Integer)
_itemsInMRU = Value
End Set
End Property
Public Property MaxRepeatRate() As Integer
Get
Return _maxRepeatRate
End Get
Set(ByVal Value As Integer)
_maxRepeatRate = Value
End Set
End Property
Public Property SettingsChanged() As Boolean
Get
Return _settingsChanged
End Get
Set(ByVal Value As Boolean)
_settingsChanged = Value
End Set
End Property
Public Property AppVersion() As String
Get
Return _appVersion
End Get
Set(ByVal Value As String)
_appVersion = Value
End Set
End Property
End Class //C# public class AppSettings{
private bool saveOnClose = true;
private string greetingText = "欢迎使用应用程序!";
private int itemsInMRU = 4;
private int maxRepeatRate = 10;
private bool settingsChanged = false;
private string appVersion = "1.0";
public bool SaveOnClose
{
get { return saveOnClose; }
set { saveOnClose = value;}
}
public string GreetingText
{
get { return greetingText; }
set { greetingText = value; }
}
public int MaxRepeatRate
{
get { return maxRepeatRate; }
set { maxRepeatRate = value; }
}
public int ItemsInMRUList
{
get { return itemsInMRU; }
set { itemsInMRU = value; }
}
public bool SettingsChanged
{
get { return settingsChanged; }
set { settingsChanged = value; }
}
public string AppVersion
{
get { return appVersion; }
set { appVersion = value; }
}
}
选项窗口上的 PropertyGrid 将使用此类,因此请将类定义添加到应用程序项目中,在添加时可创建新文件或将其添加到现有窗体源代码的下方。
要标识 PropertyGrid 显示的内容,请将 PropertyGrid.SelectedObject 属性设置为一个对象实例。然后,PropertyGrid 将完成其余的工作。每次设置 SelectedObject 时,PropertyGrid 都会刷新显示的属性。这提供了一种简单的方法来强制刷新属性,或在运行时切换对象。您还可以调用 PropertyGrid.Refresh 方法来刷新属性。
接下来,您需要更新 OptionsDialog
构造函数中的代码,以创建一个 AppSettings
对象,并将其设置为 PropertyGrid.SelectedObject 属性的值。
' Visual Basic Public Sub New() MyBase.New() OptionsPropertyGrid = New PropertyGrid() OptionsPropertyGrid.Size = New Size(300, 250) Me.Controls.Add(OptionsPropertyGrid) Me.Text = "选项对话框"' 创建 AppSettings 类并在 PropertyGrid 中显示该类。
Dim appset as AppSettings = New AppSettings()
OptionsPropertyGrid.SelectedObject = appset
End Sub //C# public OptionsDialog() { OptionsPropertyGrid = new PropertyGrid(); OptionsPropertyGrid.Size = new Size(300, 250); this.Controls.Add(OptionsPropertyGrid); this.Text = "选项对话框";// 创建 AppSettings 类并在 PropertyGrid 中显示该类。
AppSettings appset = new AppSettings();
OptionsPropertyGrid.SelectedObject = appset;
}
编译并运行该应用程序。下面的屏幕快照显示了应用程序的外观。
图 2:PropertyGrid 中选定的 AppSettings 类
您可以修改 PropertyGrid 的某些外观特征以满足自己的需要。可以更改某些属性的显示方式,甚至选择不显示某些属性。那么,如何对 PropertyGrid 进行自定义呢?
PropertyGrid 的许多外观特征都可以自定义。下面列出了其中的一部分:
本示例中的选项窗口不需要工具栏,因此可以将 ToolbarVisible 设置为 false。其余属性均保留默认设置。
要更改某些属性的显示方式,您可以对这些属性应用不同的特性。特性是用于为类型、字段、方法和属性等编程元素添加批注的声明标记,在运行时可以使用反射对其进行检索。下面列出了其中的一部分:
MaxRepeatRate
属性。
SettingsChanged
属性。
AppVersion
属性。
AppSettings
类。
现在,我们将其中的一些特性应用于 AppSettings
类,以更改属性在 PropertyGrid 中的显示方式。
' Visual Basic<DefaultPropertyAttribute("SaveOnClose")> _
Public Class AppSettings Private _saveOnClose As Boolean = True Private _greetingText As String = "欢迎使用应用程序!" Private _maxRepeatRate As Integer = 10 Private _itemsInMRU As Integer = 4 Private _settingsChanged As Boolean = False Private _appVersion As String = "1.0"<CategoryAttribute("文档设置"), _
DefaultValueAttribute(True)> _
Public Property SaveOnClose() As Boolean Get Return _saveOnClose End Get Set(ByVal Value As Boolean) SaveOnClose = Value End Set End Property<CategoryAttribute("全局设置"), _
ReadOnlyAttribute(True), _
DefaultValueAttribute("欢迎使用应用程序!")> _
Public Property GreetingText() As String Get Return _greetingText End Get Set(ByVal Value As String) _greetingText = Value End Set End Property<CategoryAttribute("全局设置"), _
DefaultValueAttribute(4)> _
Public Property ItemsInMRUList() As Integer Get Return _itemsInMRU End Get Set(ByVal Value As Integer) _itemsInMRU = Value End Set End Property<DescriptionAttribute("以毫秒表示的文本重复率。"), _
CategoryAttribute("全局设置"), _
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("版本"), _
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 //C#[DefaultPropertyAttribute("SaveOnClose")]
public class AppSettings{ private bool saveOnClose = true; private string greetingText = "欢迎使用应用程序!"; private int maxRepeatRate = 10; private int itemsInMRU = 4; private bool settingsChanged = false; private string appVersion = "1.0";[CategoryAttribute("文档设置"),
DefaultValueAttribute(true)]
public bool SaveOnClose { get { return saveOnClose; } set { saveOnClose = value;} }[CategoryAttribute("全局设置"),
ReadOnlyAttribute(true),
DefaultValueAttribute("欢迎使用应用程序!")]
public string GreetingText { get { return greetingText; } set { greetingText = value; } }[CategoryAttribute("全局设置"),
DefaultValueAttribute(4)]
public int ItemsInMRUList { get { return itemsInMRU; } set { itemsInMRU = value; } }[DescriptionAttribute("以毫秒表示的文本重复率。"),
CategoryAttribute("全局设置"),
DefaultValueAttribute(10)]
public int MaxRepeatRate { get { return maxRepeatRate; } set { maxRepeatRate = value; } }[BrowsableAttribute(false),
DefaultValueAttribute(false)]
public bool SettingsChanged { get { return settingsChanged; } set { settingsChanged = value; } }[CategoryAttribute("版本"),
DefaultValueAttribute("1.0"),
ReadOnlyAttribute(true)]
public string AppVersion { get { return appVersion; } set { appVersion = value; } } }
将这些特性应用于 AppSettings
类后,编译并运行该应用程序。下面的屏幕快照显示了应用程序的外观。
图 3:PropertyGrid 中显示的带有类别和默认值的属性
使用此版本的选项窗口后,您会注意到以下几点:
SaveOnClose
属性。
MaxRepeatRate
属性时,说明帮助窗格中将显示“以毫秒表示的文本重复率”。
SaveOnClose
属性显示在“文档设置”类别下。其他属性分别显示在“全局设置”和“版本”类别下。
SettingsChanged
属性将不再显示。
AppVersion
属性为只读。只读属性以灰显文本显示。
SaveOnClose
属性包含的值不是 true,该值将以粗体显示。PropertyGrid 使用粗体文本表示包含非默认值的属性。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com