Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# propertygrid中的TypeConverter仅从字符串转换,而不是从_C#_.net_Propertygrid_Typeconverter - Fatal编程技术网

C# propertygrid中的TypeConverter仅从字符串转换,而不是从

C# propertygrid中的TypeConverter仅从字符串转换,而不是从,c#,.net,propertygrid,typeconverter,C#,.net,Propertygrid,Typeconverter,访问propertygrid时,只调用ConvertTo方法(多次)。这将正确返回propertygrid中的“Foo!”字符串。当我点击编辑时,我得到一个异常,无法将Foo类型的对象转换为System.String类型。(不准确,已翻译)。ConvertFrom方法没有被调用,为什么?错误表明它正在尝试转换为字符串,而不是从 我会想,当我想要编辑这个对象时,它必须从Foo转换为string,然后在完成编辑后返回 StringConverter类别: public class FooTypeCo

访问propertygrid时,只调用ConvertTo方法(多次)。这将正确返回propertygrid中的“Foo!”字符串。当我点击编辑时,我得到一个异常,
无法将Foo类型的对象转换为System.String类型。
(不准确,已翻译)。ConvertFrom方法没有被调用,为什么?错误表明它正在尝试转换为字符串,而不是从

我会想,当我想要编辑这个对象时,它必须从Foo转换为string,然后在完成编辑后返回

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;
    }
}

不能繁殖;这对我来说很好;顺便说一句,您应该测试
destinationType
值的类型,但这并不能阻止它调用
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);
        }
    }
}

重新更新;这里有一个
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 { /* ... */ }

我发现它与MultileStringeditor有关,如果我禁用它,它将正常工作;您必须编写自己的编辑器-
multileStringeditor
不知道如何处理
Foo
,所以要么说“不”,要么引发并处理异常。这似乎可以做到,谢谢!我在想我可以同时使用UITypeEditor和类型转换器。@Robert-你仍然可以-p不要忘记其他一些控件(
DataGridView
etc)将只使用转换器,而不使用编辑器。