You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 rivejä
2.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. namespace BPA.UIControl.Converters
  9. {
  10. /// <summary>
  11. /// 数学计算多转换器
  12. /// </summary>
  13. public class MathMultipleConverter : IMultiValueConverter
  14. {
  15. /// <summary>
  16. /// 计算操作类型
  17. /// </summary>
  18. public enum MathOperation
  19. {
  20. /// <summary>
  21. /// 加
  22. /// </summary>
  23. Add,
  24. /// <summary>
  25. /// 减
  26. /// </summary>
  27. Subtract,
  28. /// <summary>
  29. /// 乘
  30. /// </summary>
  31. Multiply,
  32. /// <summary>
  33. /// 除
  34. /// </summary>
  35. Divide
  36. }
  37. /// <summary>
  38. /// 计算操作类型
  39. /// </summary>
  40. public MathOperation Operation { get; set; }
  41. /// <inheritdoc/>
  42. public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
  43. {
  44. if (value == null || value.Length < 2 || value[0] == null || value[1] == null) return Binding.DoNothing;
  45. if (!double.TryParse(value[0].ToString(), out double value1) || !double.TryParse(value[1].ToString(), out double value2))
  46. return 0;
  47. switch (Operation)
  48. {
  49. case MathOperation.Add:
  50. default:
  51. return value1 + value2;
  52. case MathOperation.Divide:
  53. return value1 / value2;
  54. case MathOperation.Multiply:
  55. return value1 * value2;
  56. case MathOperation.Subtract:
  57. return value1 - value2;
  58. }
  59. }
  60. /// <inheritdoc/>
  61. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  62. {
  63. throw new NotImplementedException();
  64. }
  65. }
  66. }