propertygrid 中的 TypeConverter 仅从字符串转换,而不是
访问 propertygrid 时,仅调用 ConvertTo 方法(多次)。这会正确返回“Foo!”属性网格中的字符串。当我单击编辑时,出现异常 Cannot conversion object of type Foo to type System.String.
(不完全是,已翻译)。 ConvertFrom 方法没有被调用,有什么线索吗?该错误表明它正在尝试转换为字符串,而不是从字符串。
我想当我想编辑这个对象时,它必须从 Foo 转换为字符串,并在完成编辑后返回。
StringConverter 类:
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;
}
}
Only the ConvertTo method gets called(a lot of times) when accessing the propertygrid. This correctly returns the "Foo!" string in the propertygrid. When I click to edit I get an exception Cannot convert object of type Foo to type System.String.
(not exactly, translated). The ConvertFrom method doesn't get called, any clues why? And the error indicates it's trying to convert TO a string, not from.
I would think when I want to edit this object, it has to convert from Foo to string, and when finished editing back.
StringConverter class:
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;
}
}
Property being accessed:
Foo _foo = new Foo();
[Editor(typeof(System.ComponentModel.Design.MultilineStringEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(FooTypeConverter))]
public Foo Foo {
get {
return _foo;
}
set {
_foo = value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重新更新您的信息;这是一个应该作为填充程序工作的 FooEditor:
您显然需要关联它:
Re your update; here's a
FooEditor
that should work as a shim:You'll obviously need to associate it:
无法复制;对我来说效果很好;您应该测试
destinationType
和value
类型,顺便说一句 - 但这并不能阻止它调用ConvertFrom
。您是否有一个完整的示例(可能基于以下内容)显示它不调用ConvertFrom
?Cannot reproduce; it works fine for me; you should be testing the
destinationType
and type ofvalue
, btw - but that isn't stopping it callingConvertFrom
. Do you have a complete example (perhaps based on the following) that shows it not callingConvertFrom
?