2016-09-21 2 views
0

Ich habe eine Klasse Messung, die entweder in Zoll oder Millimeter sein kann, jetzt mit dem ExpandableTypeConverter ist es ein bisschen lästig, die Messung zu ändern, also wollte ich eine benutzerdefinierte erstellen. Das Problem ist, das Propertygrid scheint es zu ignorieren. Ich habe Breakpoints gesetzt, aber nichts wird jemals aufgerufen. hier ist der Code:C# propertygrid mit benutzerdefiniertem Typkonverter, kann keine Zeichenfolge eingeben

[TypeConverter(typeof(MeasurementConverter))] 
public class Measurement 
{ 
    double mm; 
    public bool IsMM 
    { 
     get; set; 
    } 

    public double Inches 
    { 
     get { return mm/25.4; } 
     set { mm = value * 25.4; IsMM = false; } 
    } 
    public double Millimeters 
    { 
     get { return mm; } 
     set { mm = value; IsMM = true; } 
    } 

    public Measurement(double value = 0, bool ismm = true) 
    { 
     if(ismm) 
     { 
      Millimeters = value; 
     } 
     else 
     { 
      Inches = value; 
     } 
    } 

    public override string ToString() 
    { 
     if(IsMM) 
     { 
      return Millimeters.ToString(SVGElement.doubleFormat) + "mm"; 
     } 
     else 
     { 
      return Inches.ToString(SVGElement.doubleFormat) + "in"; 
     } 
    } 
} 

public class MeasurementConverter : TypeConverter 
{ 
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     return typeof(string) == destinationType; 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) 
    { 
     if(destinationType == typeof(string)) 
     { 
      Measurement m = value as Measurement; 
      return m.ToString(); 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     return typeof(string) == sourceType; 
    } 

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) 
    { 
     string str = value as string; 
     if(str != null) 
     { 
      var tmp = context.Instance; 
      Measurement m = new Measurement(); 
      if(str.EndsWith("mm")) 
      { 
       m.Millimeters = double.Parse(str.Substring(0, str.Length - 2)); 
      } 
      else if(str.EndsWith("in")) 
      { 
       m.Inches = double.Parse(str.Substring(0, str.Length - 2)); 
      } 
      else //assume mm 
      { 
       try 
       { 
        m.Millimeters = double.Parse(str); 
       } 
       catch { } 
      } 
      return m; 
     } 
     return base.ConvertFrom(context, culture, value); 
    } 
} 

ich kann eine Zeichenfolge in den Property eingeben. Es zeigt nur, was toString() zurückgibt.

Antwort

0

in einem Dingus und hatte immer noch ein typeconverter Attribut auf den Eigenschaften selbst in der Klasse, die sie enthielt.

Verwandte Themen