Guest User

Untitled

a guest
Jun 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. [ValueConversion(typeof(double), typeof(double))]
  2. public class DecreaseDoubleConverter : ValueGenericConverter<double>
  3. {
  4. protected override Func<object, CultureInfo, double> ConvertMethod =>
  5. (value, culture) =>
  6. {
  7. if (value == null)
  8. return 0;
  9. var result = System.Convert.ToDouble(value);
  10. return double.IsNaN(result) ? 0 : result;
  11. };
  12. }
  13.  
  14. public abstract class ValueGenericConverter<T> : BaseGenericConverter<T>, IValueConverter where T : new()
  15. {
  16. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  17. {
  18. Debug.Assert(targetType.IsAssignableFrom(typeof(T)), string.Format("targetType should be {typeof(T).FullName}"));
  19.  
  20. var tResult = new T();
  21. if (!Convert(value, culture, ref tResult))
  22. return DependencyProperty.UnsetValue;
  23.  
  24. ApplyParameter(parameter, culture, ref tResult);
  25. return System.Convert.ChangeType(tResult, targetType);
  26. }
  27. }
  28.  
  29. public abstract class BaseGenericConverter<T>
  30. {
  31. protected abstract Func<object, CultureInfo, T> ConvertMethod { get; }
  32. protected Func<T, T, T> ApplyParameterMethod = null;
  33. }
Add Comment
Please, Sign In to add comment