只有ConvertTo方法在访问propertygrid时被调用(很多次)。这正确地返回“Foo!” propertygrid中的字符串。当我点击编辑时,我得到一个异常Cannot convert object of type Foo to type System.String.
(不完全翻译)。 ConvertFrom方法不会被调用,为什么会有线索?并且错误表明它试图将TO转换为一个字符串,而不是从。propertygrid中的TypeConverter只能从字符串转换,而不能转换成
我想当我想编辑这个对象时,它必须从Foo转换为字符串,并且在完成编辑时返回。
类字符串转换:
public class FooTypeConverter : StringConverter {
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
return new Foo((string) value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
return "Foo!";
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
return true;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
return true;
}
}
属性被访问:
Foo _foo = new Foo();
[Editor(typeof(System.ComponentModel.Design.MultilineStringEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(FooTypeConverter))]
public Foo Foo {
get {
return _foo;
}
set {
_foo = value;
}
}
我发现它有事情做与MultilineStringEditor,如果我禁用,它正常工作。 – 2010-01-22 12:41:51
我只看到你的更新;你将不得不编写你自己的编辑器 - “MultilineStringEditor”不会**知道如何处理'Foo',所以要么说“不”,要么就是引发和处理异常。 – 2010-01-22 12:50:10