充分利用 .NET 框架的 PropertyGrid 控件(三)

翻译|其它|编辑:郝浩|2005-03-22 10:42:00.000|阅读 1040 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


对所提供类型的支持

首先,请更新 AppSettings 类,为窗口大小(Size 类型)、窗口字体(Font 类型)和工具栏颜色(Color 类型)添加新属性。

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

Private _windowSize As Size = New Size(100, 100)
Private _windowFont As Font = New Font("宋体", 9, FontStyle.Regular)
Private _toolbarColor As Color = SystemColors.Control

<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("文档设置")> _
Public Property WindowSize() As Size
Get
Return _windowSize
End Get
Set(ByVal Value As Size)
_windowSize = Value
End Set
End Property

<CategoryAttribute("文档设置")> _
Public Property WindowFont() As Font
Get
Return _windowFont
End Get
Set(ByVal Value As Font)
_windowFont = Value
End Set
End Property

<CategoryAttribute("全局设置")> _
Public Property ToolbarColor() As Color
Get
Return _toolbarColor
End Get
Set(ByVal Value As Color)
_toolbarColor = 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";

private Size windowSize = new Size(100,100);
private Font windowFont = new Font("宋体", 9, FontStyle.Regular);
private Color toolbarColor = SystemColors.Control;

[CategoryAttribute("文档设置"),
DefaultValueAttribute(true)]
public bool SaveOnClose
{
get { return saveOnClose; }
set { saveOnClose = value;}
}

[CategoryAttribute("文档设置")]
public Size WindowSize
{
get { return windowSize; }
set { windowSize = value;}
}

[CategoryAttribute("文档设置")]
public Font WindowFont
{
get {return windowFont; }
set { windowFont = value;}
}

[CategoryAttribute("全局设置")]
public Color ToolbarColor
{
get { return toolbarColor; }
set { toolbarColor = 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; }
}
}

下面的屏幕快照显示了新属性在 PropertyGrid 中的外观。



图 4:显示在 PropertyGrid 中的 .NET 框架数据类型

请注意,WindowFont 属性带有一个省略号 (...) 按钮,按下该按钮将显示字体选择对话框。此外,还可以展开该属性以显示更多的 Font 属性。某些 Font 属性提供有关字体的值和详细信息的下拉列表。您可以展开 WindowSize 属性以显示 Size 类型的更多属性。最后,请注意,ToolbarColor 属性包含一个选定颜色的样本,以及一个用于选择不同颜色的自定义下拉列表。对于这些以及其他数据类型,.NET 框架提供了其他的类,可以使在 PropertyGrid 中的编辑更加容易。

对自定义类型的支持

现在,您需要在 AppSettings 类中添加另外两个属性,即 DefaultFileName 和 SpellCheckOptions。 DefaultFileName 属性用于获取或设置字符串;SpellCheckOptions 属性用于获取或设置 SpellingOptions 类的实例。

SpellingOptions 类是一个新类,用于管理应用程序的拼写检查属性。对于何时创建单独的类以管理对象的属性,并没有严格的规定,而取决于您的整个类设计。将 SpellingOptions 类定义添加到应用程序项目中 - 可以添加到新文件中,也可以添加到窗体源代码的下方。

' Visual Basic

<DescriptionAttribute("展开以查看应用程序的拼写选项。")> _
Public Class SpellingOptions
Private _spellCheckWhileTyping As Boolean = True
Private _spellCheckCAPS As Boolean = False
Private _suggestCorrections As Boolean = True

<DefaultValueAttribute(True)> _
Public Property SpellCheckWhileTyping() As Boolean
Get
Return _spellCheckWhileTyping
End Get
Set(ByVal Value As Boolean)
_spellCheckWhileTyping = Value
End Set
End Property

<DefaultValueAttribute(False)> _
Public Property SpellCheckCAPS() As Boolean
Get
Return _spellCheckCAPS
End Get
Set(ByVal Value As Boolean)
_spellCheckCAPS = Value
End Set
End Property

<DefaultValueAttribute(True)> _
Public Property SuggestCorrections() As Boolean
Get
Return _suggestCorrections
End Get
Set(ByVal Value As Boolean)
_suggestCorrections = Value
End Set
End Property
End Class


//C#

[DescriptionAttribute("展开以查看应用程序的拼写选项。")]
public class SpellingOptions{
private bool spellCheckWhileTyping = true;
private bool spellCheckCAPS = false;
private bool suggestCorrections = true;

[DefaultValueAttribute(true)]
public bool SpellCheckWhileTyping
{
get { return spellCheckWhileTyping; }
set { spellCheckWhileTyping = value; }
}

[DefaultValueAttribute(false)]
public bool SpellCheckCAPS
{
get { return spellCheckCAPS; }
set { spellCheckCAPS = value; }
}
[DefaultValueAttribute(true)]
public bool SuggestCorrections
{
get { return suggestCorrections; }
set { suggestCorrections = value; }
}
}


再次编译并运行选项窗口应用程序。下面的屏幕快照显示了应用程序的外观。



图 5:在 PropertyGrid 中显示的不带类型转换器的自定义数据类型

请注意 SpellcheckOptions 属性的外观。与 .NET 框架类型不同,它不展开或显示自定义的字符串表示。如果要在自己的复杂类型中提供与 .NET 框架类型相同的编辑体验,该如何处理呢?.NET 框架类型使用 TypeConverter 和 UITypeEditor 类提供大部分 PropertyGrid 编辑支持,您也可以使用这些类。

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP