PropertyGrid控件(6) 自定义属性类型

概要:如何自定义一个类型 (type)。可以通过生成一个新的类,让PropertyGrid 关联对象的某个属性的类型就是这个新的类。

Support for Custom Types

Now add two more properties to the AppSettings class, one called DefaultFileName and the other called SpellCheckOptions. The DefaultFileName property gets or sets a string and the SpellCheckOptions property gets or sets an instance of the SpellingOptions class.

The SpellingOptions class is a new class that will manage the application's spell checking properties. There is no hard and fast rule about when to create a separate class to manage an object's properties; it depends upon your overall class design. Add the SpellingOptions class definition to your application project, either in a new file or at the bottom of the source code for the form.

<DescriptionAttribute("Expand to see the spelling options for the application.")> _
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

Compile and run the options window application again. The following screen shot shows what it should look like.

propertygrid, custom types

Figure 5. Custom data types without a type converter, displayed in the PropertyGrid

Notice how the SpellcheckOptions property looks. Unlike the .NET Framework types, it doesn't expand or display a custom string representation. What if you want to provide the same editing experience that the .NET Framework types have to your own complex type? The .NET Framework types use the TypeConverter and UITypeEditor classes to provide most of the PropertyGrid editing support, and you can use them too.


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

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


© 2008 woyouxian.net 版权所有 Contact Us