PropertyGrid使用小记
PropertyGrid进行编辑的时候,还是比较方便的,至少不进行界面布局了,目前在一个小工具中需要实现下面的效果
数据库类型是enum
需要实现一个Convert类,基类是ExpandableObjectConverter
重写基类的CanConvertFrom,CanConvertTo,ConvertFrom,ConvertTo方法即可实现
Public Class DBInfoConverter Inherits ExpandableObjectConverter Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.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 Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean If destinationType Is GetType(String) Then Return True End If Return MyBase.CanConvertTo(context, destinationType) End Function Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object Dim db As New DBInfo If TypeOf value Is String Then Dim str As String = value.ToString Dim chars() As String = str.Split(","c) If chars.Length <> 5 Then Throw New ArgumentException("无效的值", "value") End If Dim stringTypeConvert = TypeDescriptor.GetConverter(GetType(String)) Dim enumTypeConvert = TypeDescriptor.GetConverter(GetType(DBInfoType)) db.服务器地址 = stringTypeConvert.ConvertToString(context, culture, chars(0)) db.用户名 = stringTypeConvert.ConvertToString(context, culture, chars(1)) db.密码 = stringTypeConvert.ConvertToString(context, culture, chars(2)) db.数据库 = stringTypeConvert.ConvertToString(context, culture, chars(3)) db.数据库类型 = CType(enumTypeConvert.ConvertFrom(chars(4)), DBInfoType) End If Return db End Function Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, _
ByVal value As Object, ByVal destinationType As System.Type) As Object If value Is Nothing Then value = New DBInfo 'Return MyBase.ConvertTo(context, culture, value, destinationType) End If If Not TypeOf value Is DBInfo Then Throw New ArgumentException("无效的对象", "value") End If Dim db = CType(value, DBInfo) Dim stringTypeConvert = TypeDescriptor.GetConverter(GetType(String)) Dim enumTypeConvert = TypeDescriptor.GetConverter(GetType(DBInfoType)) Dim chars() As String = {stringTypeConvert.ConvertToString(context, culture, db.服务器地址), _ stringTypeConvert.ConvertToString(context, culture, db.用户名), _ stringTypeConvert.ConvertToString(context, culture, db.密码), _ stringTypeConvert.ConvertToString(context, culture, db.数据库), _ CStr(enumTypeConvert.ConvertTo(db.数据库类型, GetType(String)))} Return String.Join(culture.TextInfo.ListSeparator, chars) End Function End Class
相关资料:
http://msdn.microsoft.com/zh-cn/library/ayybcxe5.aspx
http://msdn.microsoft.com/zh-cn/library/system.componentmodel.notifyparentpropertyattribute.aspx
http://www.cnblogs.com/wmj/archive/2008/05/16/1199823.html
http://msdn.microsoft.com/zh-tw/vcsharp/system.componentmodel.enumconverter(zh-cn).aspx