ThicknessConverter.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ThicknessConverter.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Converts from <see cref="Thickness" /> to the maximum thicknesses.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. namespace OxyPlot.Avalonia.Converters
  10. {
  11. using global::Avalonia;
  12. using global::Avalonia.Data.Converters;
  13. using System;
  14. using System.Globalization;
  15. /// <summary>
  16. /// Converts from <see cref="Thickness" /> to the maximum thicknesses.
  17. /// </summary>
  18. /// <remarks>This is used in the <see cref="TrackerControl" /> to convert BorderThickness properties to Path.StrokeThickness (double).
  19. /// The maximum thickness value is used.</remarks>
  20. public class ThicknessConverter : IValueConverter
  21. {
  22. public static readonly ThicknessConverter Instance = new ThicknessConverter();
  23. /// <summary>
  24. /// Converts a value.
  25. /// </summary>
  26. /// <param name="value">The value produced by the binding source.</param>
  27. /// <param name="targetType">The type of the binding target property.</param>
  28. /// <param name="parameter">The converter parameter to use.</param>
  29. /// <param name="culture">The culture to use in the converter.</param>
  30. /// <returns>A converted value. If the method returns <c>null</c>, the valid <c>null</c> value is used.</returns>
  31. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  32. {
  33. if (value is Thickness t && targetType == typeof(double))
  34. {
  35. return Math.Max(Math.Max(t.Left, t.Right), Math.Max(t.Top, t.Bottom));
  36. }
  37. return value;
  38. }
  39. /// <summary>
  40. /// Converts a value.
  41. /// </summary>
  42. /// <param name="value">The value that is produced by the binding target.</param>
  43. /// <param name="targetType">The type to convert to.</param>
  44. /// <param name="parameter">The converter parameter to use.</param>
  45. /// <param name="culture">The culture to use in the converter.</param>
  46. /// <returns>A converted value. If the method returns <c>null</c>, the valid <c>null</c> value is used.</returns>
  47. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  48. {
  49. return null;
  50. }
  51. }
  52. }