2010-01-22 86 views
5

只有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; 
    } 
} 
+0

我發現它有事情做與MultilineStringEditor,如果我禁用,它正常工作。 – 2010-01-22 12:41:51

+0

我只看到你的更新;你將不得不編寫你自己的編輯器 - 「MultilineStringEditor」不會**知道如何處理'Foo',所以要麼說「不」,要麼就是引發和處理異常。 – 2010-01-22 12:50:10

回答

5

重新更新;這裏有一個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 { /* ... */ } 
+0

這似乎這樣做,謝謝!我想我可以使用UITypeEditor和TypeConverter。 – 2010-01-22 15:07:01

+0

@Robert - 你仍然可以;-p不要忘記一些其他控件('DataGridView'等)將只使用轉換器,而不是編輯器。 – 2010-01-22 16:25:17

0

無法重現;這對我來說可以;你應該測試destinationType和類型value,順便說一句 - 但這並不能阻止它呼籲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); 
     } 
    } 
}