概要:用 ExpandableObjectConverter 创建自定义扩展属性类型,做成类似
To get the PropertyGrid to expand the SpellingOptions property, you will need to create a TypeConverter. A TypeConverter provides a way to convert from one type to another. The PropertyGrid uses the TypeConverter to convert your object type to a String, which it uses to display the object value in the grid. During editing, the TypeConverter converts back to your object type from a String. The .NET Framework provides the ExpandableObjectConverter class to make this easier.
1. Create a class that inherits from ExpandableObjectConverter.
Public Class SpellingOptionsConverter
Inherits ExpandableObjectConverter
End Class
2.Override the CanConvertTo method and return true if the destinationType parameter is the same type as the class that uses this type converter (the SpellingOptions class in your example); otherwise, return the value of the base class CanConvertTo method.
Public Overloads Overrides Function CanConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean
If (destinationType Is GetType(SpellingOptions)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, destinationType)
End Function
3. Override the ConvertTo method and ensure that the destinationType parameter is a String and that the value is the same type as the class that uses this type converter (the SpellingOptions class in your example). If either case is false, return the value of the base class ConvertTo method; otherwise return a string representation of the value object. The string representation needs to separate each property of your class with a unique delimiter. Since the whole string will be displayed in the PropertyGrid you will want to choose a delimiter that doesn't detract from the readability; commas usually work well.
Public Overloads Overrides Function ConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As CultureInfo, _
ByVal value As Object, _
ByVal destinationType As System.Type) _
As Object
If (destinationType Is GetType(System.String) _
AndAlso TypeOf value Is SpellingOptions) Then Dim so As SpellingOptions = CType(value, SpellingOptions) Return "Check while typing: " & so.SpellCheckWhileTyping & _
", check CAPS: " & so.SpellCheckCAPS & _
", suggest corrections: " & so.SuggestCorrections
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
4. (Optional) You can enable editing of the object's string representation in the grid by specifying that your type converter can convert from a string. To do this, first override the CanConvertFrom method and return true if the source Type parameter is of type String; otherwise, return the value of the base class CanConvertFrom method.
Public Overloads Overrides Function CanConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal sourceType As System.Type) As Boolean
If (sourceType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
5. To enable editing of the object's base class, you also need to override the ConvertFrom method and ensure that the value parameter is a String. If it is not a String, return the value of the base class ConvertFrom method; otherwise return a new instance of your class (the SpellingOptions class in your example) based on the value parameter. You will need to parse the values for each property of your class from the value parameter. Knowing the format of the delimited string you created in the ConvertTo method will help you perform the parsing.
Public Overloads Overrides Function ConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As CultureInfo, _
ByVal value As Object) As Object If (TypeOf value Is String) Then
Try
Dim s As String = CStr(value)
Dim colon As Integer = s.IndexOf(":")
Dim comma As Integer = s.IndexOf(",") If (colon <> -1 AndAlso comma <> -1) Then
Dim checkWhileTyping As String = s.Substring(colon + 1, _
(comma - colon - 1)) colon = s.IndexOf(":", comma + 1)
comma = s.IndexOf(",", comma + 1) Dim checkCaps As String = s.Substring(colon + 1, _
(comma - colon - 1)) colon = s.IndexOf(":", comma + 1) Dim suggCorr As String = s.Substring(colon + 1) Dim so As SpellingOptions = New SpellingOptions() so.SpellCheckWhileTyping = Boolean.Parse(checkWhileTyping)
so.SpellCheckCAPS = Boolean.Parse(checkCaps)
so.SuggestCorrections = Boolean.Parse(suggCorr) Return so
End If
Catch
Throw New ArgumentException( _
"Can not convert '" & CStr(value) & _
"' to type SpellingOptions") End Try
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
6. Now that you have a type converter class, you need to identify the target class that will use it. You do this by applying the TypeConverterAttribute to the target class (the SpellingOptions class in your example).
' The TypeConverter attribute applied to the SpellingOptions class.
<TypeConverter(GetType(SpellingOptionsConverter)), _
DescriptionAttribute("Expand to see the spelling options for the application.")> _
Public Class SpellingOptions
...
End Class
Compile and run the options window application again. The following screen shot shows how the options window should now look.

Note If you only want the expandable object support, but not the custom string representation, you can simply apply the TypeConverterAttribute to your class. Specify ExpandableObjectConverter as the type converter type.
上一页: PropertyGrid控件(6) 自定义属性类型 返回上级目录: IT知识文章 下一页: PropertyGrid控件(8) 下拉框属性
© 2008 woyouxian.net 版权所有 Contact Us