【问题标题】:TypeConverter in propertygrid only converts from string, not topropertygrid 中的 TypeConverter 仅从字符串转换,而不是
【发布时间】:2010-01-22 11:39:58
【问题描述】:

在访问 propertygrid 时,只有 ConvertTo 方法被调用(很多次)。这会正确返回“Foo!”属性网格中的字符串。当我单击编辑时,我得到一个异常Cannot convert 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;
    }
}

【问题讨论】:

  • 我发现它与 MultilineStringEditor 有关,如果我禁用它,它可以正常工作。
  • 我才看到你的更新;您将不得不编写自己的编辑器 - MultilineStringEditor会知道如何处理 Foo,所以要么说“不”,要么正在引发和处理异常.

标签: c# .net propertygrid typeconverter


【解决方案1】:

您的更新;这是一个FooEditor,它应该可以用作垫片:

class FooEditor : UITypeEditor
{
    MultilineStringEditor ed = new MultilineStringEditor();
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        Foo foo = value as Foo;
        if (foo != null)
        {
            value = new Foo((string)ed.EditValue(provider, foo.Value));
        }
        return value;        
    }
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return ed.GetEditStyle();
    }
    public override bool  IsDropDownResizable {
        get { return ed.IsDropDownResizable; }
    }
}

你显然需要关联它:

[TypeConverter(typeof(FooTypeConverter))]
[Editor(typeof(FooEditor), typeof(UITypeEditor))]
class Foo { /* ... */ }

【讨论】:

  • 好像可以,谢谢!我在想我可以同时使用 UITypeEditor 和 TypeConverter。
  • @Robert - 你仍然可以;-p 不要忘记其他一些控件(DataGridView 等)只会使用转换器,而不是编辑器。
【解决方案2】:

无法复制;这对我来说可以;您应该测试destinationTypevalue 的类型,顺便说一句——但这并不能阻止它调用ConvertFrom。您是否有一个完整的示例(可能基于以下内容)表明它调用ConvertFrom

using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;
public class FooTypeConverter : StringConverter {
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return new Foo("FooTypeConverter.ConvertFrom: " + Convert.ToString(value));
    }
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        return "FooTypeConverter.ConvertTo: " + ((Foo)value).Value;
    }
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return true;
    }
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return true;
    }
}
[TypeConverter(typeof(FooTypeConverter))]
class Foo
{
    public string Value { get; set; }
    public Foo(string value) { Value = value; }

    public override string ToString()
    {
        return "Foo.ToString";
    }
}
class Test
{
    public Foo Foo { get; set; }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        using(Form form = new Form())
        using (PropertyGrid grid = new PropertyGrid())
        {
            grid.Dock = DockStyle.Fill;
            grid.SelectedObject = new Test { Foo = new Foo("Main") };
            form.Controls.Add(grid);
            Application.Run(form);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多