2010-01-22 13 views
5

Sadece convertTo yöntemi, propertygrid'e erişirken (çoğu kez) çağrılır. Bu doğru "Foo!" propertygrid dizesi. Düzenlemek için tıkladığımda bir istisna Cannot convert object of type Foo to type System.String. (tam olarak tercüme edilmez) alıyorum. ConvertFrom yöntemi çağrılmadı, neden herhangi bir ipucu? Ve hata, bir dizgeden TO’yu dönüştürmeyi denediğini gösterir.PropertyGrid'deki TypeConverter sadece dize dönüştürür, değil

Bu nesneyi düzenlemek istediğimde, Foo'dan string'e ve geri düzenlemeyi tamamladığında dönüştürmem gerektiğini düşünürdüm.

StringConverter sınıfı:

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

Mülkiyet erişilen:

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

öğrendim bunu devre dışı eğer, MultilineStringEditor ile ilgili bir şey vardır, doğru çalışıyor. –

+0

Sadece güncellemenizi daha yeni gördüm; Kendi editörünüzü yazmanız gerekecek - 'MultilineStringEditor' ** ** 'foo'yu nasıl işleyeceğini bilen, yani' hayır 'diyerek ya da bir istisna kaldırılmakta ve ele alınmaktadır. –

cevap

5

Güncelleştirmeyi yeniden; Belli ki ilişkilendirmek gerekir

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

: Burada bir takoz halinde çalışması gerektiğini bir FooEditor var

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

Böyle görünüyor, teşekkürler! BOTH bir UITypeEditor ve bir TypeConverter kullanabileceğimi düşünüyordum. –

+0

@Robert - hala yapamazsınız; -p Bazı diğer kontrollerin ('DataGridView' vb.) Sadece editörü değil editörü kullanacağını unutmayın. –

0

yeniden Can not; benim için iyi çalışıyor; destinationType ve value, btw türlerini test etmelisiniz - ancak bu, ConvertFrom numaralı telefonu arayarak durmuyor. ConvertFrom numaralı telefonu arayarak 'u göstermeyen tam bir örneğiniz var mı (belki de aşağıdakilere göre)?

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); 
     } 
    } 
} 
İlgili konular