C# propertygrid 扩展

添加可展开属性支持

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

提供可展开对象支持

  1. 创建一个从 ExpandableObjectConverter 继承而来的类。 
    1. public class SpellingOptionsConverter:ExpandableObjectConverter   
    2. {   }  
  2. 如果 destinationType 参数与使用此类型转换器的类(示例中的 SpellingOptions 类)的类型相同,则覆盖 CanConvertTo 方法并返回 true ;否则返回基类 CanConvertTo 方法的值。 
    1. public override bool CanConvertTo(ITypeDescriptorContext context,  
    2.                                    System.Type destinationType)   
    3. {  
    4.      if (destinationType == typeof(SpellingOptions))  
    5.          return true;  
    6.      return base.CanConvertTo(context, destinationType);  
    7. }  
  3. 覆盖 ConvertTo 方法,并确保 destinationType 参数是一个 String ,并且值的类型与使用此类型转换器的类(示例中的 SpellingOptions 类)相同。如果其中任一情况为 false ,都将返回基类 ConvertTo 方法的值;否则,返回值对象的字符串表示。字符串表示需要使用唯一分隔符将类的每个属性隔开。由于整个字符串都将显示在 PropertyGrid 中,因此需要选择一个不会影响可读性的分隔符,逗号的效果通常比较好。 
    1. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,  
    2.     object value, System.Type destinationType)   
    3. {  
    4.      if (destinationType == typeof(System.String) &&   
    5.           value is SpellingOptions){  
    6.          SpellingOptions so = (SpellingOptions)value;  
    7.          return "在键入时检查:" + so.SpellCheckWhileTyping +   
    8.                 ",检查大小写: " + so.SpellCheckCAPS +  
    9.                 ",建议更正: " + so.SuggestCorrections;  
    10.      }  
    11.      return base.ConvertTo(context, culture, value, destinationType);  
    12. }  
  4. (可选)通过指定类型转换器可以从字符串进行转换,您可以启用网格中对象字符串表示的编辑。要执行此操作,首先需要覆盖 CanConvertFrom 方法并返回 true (如果源 Type 参数为 String 类型);否则,返回基类 CanConvertFrom 方法的值。 
    1. public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)   
    2. {  
    3.      if (sourceType == typeof(string))  
    4.          return true;  
    5.      return base.CanConvertFrom(context, sourceType);  
    6. }  
  5. 要启用对象基类的编辑,同样需要覆盖 ConvertFrom 方法并确保值参数是一个 String 。如果不是String ,将返回基类 ConvertFrom 方法的值;否则,返回基于值参数的类(示例中的 SpellingOptions类)的新实例。您需要根据值参数解析类的每个属性的值。了解在 ConvertTo 方法中创建的分隔字符串的格式将有助于您的解析。 
    1. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)   
    2. {  
    3.      if (value is string) {  
    4.          try {  
    5.              string s = (string) value;  
    6.              int colon = s.IndexOf(':');  
    7.              int comma = s.IndexOf(',');  
    8.              if (colon != -1 && comma != -1) {  
    9.                  string checkWhileTyping = s.Substring(colon + 1 , (comma - colon - 1));  
    10.                  colon = s.IndexOf(':', comma + 1);  
    11.                  comma = s.IndexOf(',', comma + 1);  
    12.                  string checkCaps = s.Substring(colon + 1 , (comma - colon -1));  
    13.                  colon = s.IndexOf(':', comma + 1);  
    14.                  string suggCorr = s.Substring(colon + 1);  
    15.                  SpellingOptions so = new SpellingOptions();  
    16.                  so.SpellCheckWhileTyping =Boolean.Parse(checkWhileTyping);  
    17.                  so.SpellCheckCAPS = Boolean.Parse(checkCaps);  
    18.                  so.SuggestCorrections = Boolean.Parse(suggCorr);  
    19.                  return so;  
    20.              }  
    21.          }  
    22.          catch {  
    23.              throw new ArgumentException(  
    24.                  "无法将“" + (string)value +   
    25.                                     "”转换为 SpellingOptions 类型");  
    26.          }  
    27.      }     
    28.      return base.ConvertFrom(context, culture, value);  
    29. }  
  6. 现在已经有了一个类型转换器类,下面您需要确定使用该类的目标类。您可以通过将TypeConverterAttribute 应用到目标类(示例中的 SpellingOptions 类)来执行此操作。 
    1. // 应用于 SpellingOptions 类的 TypeConverter 特性。  
    2. [TypeConverterAttribute(typeof(SpellingOptionsConverter)),  
    3. DescriptionAttribute(“展开以查看应用程序的拼写选项。")]  
    4. public class SpellingOptions{ ... }  

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

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

posted @ 2011-09-05 09:27  Tammie-锴  阅读(957)  评论(0编辑  收藏  举报