public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { ObjectConverter dc = ObjectConverters.Find(targetType); if (dc != null && value is string s) { try { return(dc.Construct(s)); } catch (Exception e) { Debug.WriteLine("Unable to parse " + value.ToString() + ": " + e.Message); } } return(null); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null && targetType == typeof(string)) { ObjectConverter dc = ObjectConverters.Find(value.GetType()); if (dc != null) { try { return(dc.Format(value)); } catch (Exception e) { Debug.WriteLine("Unable to format " + value.ToString() + ": " + e.Message); } } } return(null); }
public void TestAdd() { ObjectConverters.Add(new ObjectConverter(typeof(System.Drawing.Size), new Type[] { typeof(int), typeof(int) }, (d) => { var s = (System.Drawing.Size)d; return(new object[] { s.Width, s.Height }); }, (ol) => new System.Drawing.Size((int)ol[0], (int)ol[1]), "({0},{1})" )); ObjectConverters.Add(new ObjectConverter(typeof(Complex), new Type[] { typeof(double), typeof(double) }, (d) => { var c = (Complex)d; return(new object[] { c.Real, c.Imaginary }); }, (ol) => new Complex((double)ol[0], (double)ol[1]), "({0},{1})")); Assert.Equal(2, ObjectConverters.Instance.Count); Assert.Equal(typeof(Complex), ObjectConverters.Find(typeof(Complex)).Foundation); }