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

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

概述:

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


添加可展开属性支持

要使 PropertyGrid 能够展开 SpellingOptions 属性,您需要创建 TypeConverter。TypeConverter 提供了从一种类型转换为另一种类型的方法。PropertyGrid 使用 TypeConverter 将对象类型转换为 String,并使用该 String 在网格中显示对象值。在编辑过程中,TypeConverter 会将 String 转换回对象类型。.NET 框架提供的 ExpandableObjectConverter 类可以简化这一过程。
 

提供可展开对象支持

  1. 创建一个从 ExpandableObjectConverter 继承而来的类。
     
    ' Visual Basic

    Public Class SpellingOptionsConverter
    Inherits ExpandableObjectConverter
    End Class


    //C#

    public class SpellingOptionsConverter:ExpandableObjectConverter
    { }
  2. 如果 destinationType 参数与使用此类型转换器的类(示例中的 SpellingOptions 类)的类型相同,则覆盖 CanConvertTo 方法并返回 true;否则返回基类 CanConvertTo 方法的值。
     
    ' Visual Basic

    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.CanConvertTo(context, destinationType)
    End Function
    //C#

    public override bool CanConvertTo(ITypeDescriptorContext context,
    System.Type destinationType)
    {
    if (destinationType == typeof(SpellingOptions))
    return true;

    return base.CanConvertTo(context, destinationType);
    }

     

  3. 覆盖 ConvertTo 方法,并确保 destinationType 参数是一个 String,并且值的类型与使用此类型转换器的类(示例中的 SpellingOptions 类)相同。如果其中任一情况为 false,都将返回基类 ConvertTo 方法的值;否则,返回值对象的字符串表示。字符串表示需要使用唯一分隔符将类的每个属性隔开。由于整个字符串都将显示在 PropertyGrid 中,因此需要选择一个不会影响可读性的分隔符,逗号的效果通常比较好。
     
    ' Visual Basic

    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 "在键入时检查: " & so.SpellCheckWhileTyping & _
    ",检查大小写: " & so.SpellCheckCAPS & _
    ",建议更正: " & so.SuggestCorrections
    End If
    Return MyBase.ConvertTo(context, culture, value, destinationType)
    End Function
    //C#

    public override object ConvertTo(ITypeDescriptorContext context,
    CultureInfo culture,
    object value,
    System.Type destinationType)
    {
    if (destinationType == typeof(System.String) &&
    value is SpellingOptions){

    SpellingOptions so = (SpellingOptions)value;

    return "在键入时检查:" + so.SpellCheckWhileTyping +
    ",检查大小写: " + so.SpellCheckCAPS +
    ",建议更正: " + so.SuggestCorrections;
    }
    return base.ConvertTo(context, culture, value, destinationType);
    }

     

  4. (可选)通过指定类型转换器可以从字符串进行转换,您可以启用网格中对象字符串表示的编辑。要执行此操作,首先需要覆盖 CanConvertFrom 方法并返回 true(如果源 Type 参数为 String 类型);否则,返回基类 CanConvertFrom 方法的值。
     
    ' Visual Basic

    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
    //C#

    public override bool CanConvertFrom(ITypeDescriptorContext context,
    System.Type sourceType)
    {
    if (sourceType == typeof(string))
    return true;

    return base.CanConvertFrom(context, sourceType);
    }

     

  5. 要启用对象基类的编辑,同样需要覆盖 ConvertFrom 方法并确保值参数是一个 String。如果不是 String,将返回基类 ConvertFrom 方法的值;否则,返回基于值参数的类(示例中的 SpellingOptions 类)的新实例。您需要根据值参数解析类的每个属性的值。了解在 ConvertTo 方法中创建的分隔字符串的格式将有助于您的解析。
     
    ' Visual Basic

    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( _
    "无法将“" & CStr(value) & _
    "”转换为 SpellingOptions 类型")

    End Try
    End If
    Return MyBase.ConvertFrom(context, culture, value)
    End Function
    //C#

    public override object ConvertFrom(ITypeDescriptorContext context,
    CultureInfo culture, object value)
    {
    if (value is string) {
    try {
    string s = (string) value;
    int colon = s.IndexOf(':');
    int comma = s.IndexOf(',');

    if (colon != -1 && comma != -1) {
    string checkWhileTyping = s.Substring(colon + 1 ,
    (comma - colon - 1));

    colon = s.IndexOf(':', comma + 1);
    comma = s.IndexOf(',', comma + 1);

    string checkCaps = s.Substring(colon + 1 ,
    (comma - colon -1));

    colon = s.IndexOf(':', comma + 1);

    string suggCorr = s.Substring(colon + 1);

    SpellingOptions so = new SpellingOptions();

    so.SpellCheckWhileTyping =Boolean.Parse(checkWhileTyping);
    so.SpellCheckCAPS = Boolean.Parse(checkCaps);
    so.SuggestCorrections = Boolean.Parse(suggCorr);

    return so;
    }
    }
    catch {
    throw new ArgumentException(
    "无法将“" + (string)value +
    "”转换为 SpellingOptions 类型");
    }
    }
    return base.ConvertFrom(context, culture, value);
    }
  6. 现在已经有了一个类型转换器类,下面您需要确定使用该类的目标类。您可以通过将 TypeConverterAttribute 应用到目标类(示例中的 SpellingOptions 类)来执行此操作。
     
    ' Visual Basic

    ' 应用于 SpellingOptions 类的 TypeConverter 特性。
    <TypeConverter(GetType(SpellingOptionsConverter)), _
    DescriptionAttribute("展开以查看应用程序的拼写选项。")> _
    Public Class SpellingOptions
    ...
    End Class


    //C#

    // 应用于 SpellingOptions 类的 TypeConverter 特性。
    [TypeConverterAttribute(typeof(SpellingOptionsConverter)),
    DescriptionAttribute("展开以查看应用程序的拼写选项。")]
    public class SpellingOptions{ ... }
     

     

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



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

       注意: 如果只需要可展开对象支持,而不需要自定义字符串表示,则只需将 TypeConverterAttribute 应用到类中。将 ExpandableObjectConverter 指定为类型转换器类型。
     

标签:

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


为你推荐

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


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP