C# propertygrid 扩展
添加可展开属性支持
要使 PropertyGrid 能够展开 SpellingOptions
属性,您需要创建 TypeConverter 。TypeConverter 提供了从一种类型转换为另一种类型的方法。PropertyGrid 使用 TypeConverter 将对象类型转换为 String ,并使用该 String 在网格中显示对象值。在编辑过程中,TypeConverter 会将 String 转换回对象类型。.NET 框架提供的 ExpandableObjectConverter 类可以简化这一过程。
提供可展开对象支持
- 创建一个从 ExpandableObjectConverter 继承而来的类。
- public class SpellingOptionsConverter:ExpandableObjectConverter
- { }
- 如果
destinationType
参数与使用此类型转换器的类(示例中的SpellingOptions
类)的类型相同,则覆盖 CanConvertTo 方法并返回 true ;否则返回基类 CanConvertTo 方法的值。
- public override bool CanConvertTo(ITypeDescriptorContext context,
- System.Type destinationType)
- {
- if (destinationType == typeof(SpellingOptions))
- return true;
- return base.CanConvertTo(context, destinationType);
- }
- 覆盖 ConvertTo 方法,并确保
destinationType
参数是一个 String ,并且值的类型与使用此类型转换器的类(示例中的SpellingOptions
类)相同。如果其中任一情况为 false ,都将返回基类 ConvertTo 方法的值;否则,返回值对象的字符串表示。字符串表示需要使用唯一分隔符将类的每个属性隔开。由于整个字符串都将显示在 PropertyGrid 中,因此需要选择一个不会影响可读性的分隔符,逗号的效果通常比较好。
- 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);
- }
- (可选)通过指定类型转换器可以从字符串进行转换,您可以启用网格中对象字符串表示的编辑。要执行此操作,首先需要覆盖 CanConvertFrom 方法并返回 true (如果源 Type 参数为 String 类型);否则,返回基类 CanConvertFrom 方法的值。
- public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)
- {
- if (sourceType == typeof(string))
- return true;
- return base.CanConvertFrom(context, sourceType);
- }
- 要启用对象基类的编辑,同样需要覆盖 ConvertFrom 方法并确保值参数是一个 String 。如果不是String ,将返回基类 ConvertFrom 方法的值;否则,返回基于值参数的类(示例中的
SpellingOptions
类)的新实例。您需要根据值参数解析类的每个属性的值。了解在 ConvertTo 方法中创建的分隔字符串的格式将有助于您的解析。
- 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);
- }
- 现在已经有了一个类型转换器类,下面您需要确定使用该类的目标类。您可以通过将TypeConverterAttribute 应用到目标类(示例中的
SpellingOptions
类)来执行此操作。
- // 应用于 SpellingOptions 类的 TypeConverter 特性。
- [TypeConverterAttribute(typeof(SpellingOptionsConverter)),
- DescriptionAttribute(“展开以查看应用程序的拼写选项。")]
- public class SpellingOptions{ ... }
再次编译并运行选项窗口应用程序。下面的屏幕快照显示了选项窗口目前的外观。