MathAddConverter.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Avalonia.Data.Converters;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ShakerApp.Convert
  9. {
  10. public class MathAddConverter : IValueConverter
  11. {
  12. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  13. {
  14. // For add this is simple. just return the sum of the value and the parameter.
  15. // You may want to validate value and parameter in a real world App
  16. if (value is double v && double.TryParse(parameter!.ToString(), out var val))
  17. {
  18. return v + val;
  19. }
  20. return 0;
  21. }
  22. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  23. {
  24. // If we want to convert back, we need to subtract instead of add.
  25. if (value is double v && double.TryParse(parameter!.ToString(), out var val))
  26. {
  27. return v - val;
  28. }
  29. return 0;
  30. }
  31. }
  32. public class SubtractionConverter : IValueConverter
  33. {
  34. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  35. {
  36. // For add this is simple. just return the sum of the value and the parameter.
  37. // You may want to validate value and parameter in a real world App
  38. if (value is double v && double.TryParse(parameter!.ToString(), out var val))
  39. {
  40. return v - val;
  41. }
  42. return 0;
  43. }
  44. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  45. {
  46. // If we want to convert back, we need to subtract instead of add.
  47. if (value is double v && double.TryParse(parameter!.ToString(), out var val))
  48. {
  49. return v + val;
  50. }
  51. return 0;
  52. }
  53. }
  54. public class MultiConverter : IValueConverter
  55. {
  56. public static MultiConverter Instance { get; } = new MultiConverter();
  57. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  58. {
  59. if(value is double v && double.TryParse(parameter!.ToString(),out var val))
  60. {
  61. return v * val;
  62. }
  63. return 0;
  64. }
  65. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. }
  70. }