RingProgressArcConverter.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Data;
  9. using System.Windows.Media;
  10. using Venus_Themes.CustomControls;
  11. namespace Venus_Themes.Converters
  12. {
  13. public class RingProgressArcConverter : IMultiValueConverter
  14. {
  15. // 注意,因为这里使用Path绘制圆环, 所以要把描边宽度大小考虑进去. 所有点的x、y偏移 半个描边宽度
  16. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  17. {
  18. if (values == null) { return null; }
  19. double value;
  20. if (values[0] is double width
  21. && values[1] is double height
  22. && width > 0 && height > 0
  23. && values[2] is double storkeWidth)
  24. {
  25. width -= storkeWidth;
  26. height -= storkeWidth;
  27. value = values.Length == 4 ? System.Convert.ToDouble(values[3]) : 1d;
  28. if (value == 0) return "";
  29. var startAngle = -90d;
  30. var endAngle = Math.Min(value * 360 - 90, 269);
  31. var radius = Math.Min(width, height) * 0.5;
  32. var start = startAngle.AngleToPoint(radius, storkeWidth * 0.5);
  33. var end = endAngle.AngleToPoint(radius, storkeWidth * 0.5);
  34. var dataStr = $"M {start.X},{start.Y} A {radius},{radius} 0 {(endAngle - startAngle >= 180 ? 1 : 0)} 1 {end.X},{end.Y}";
  35. var converter = TypeDescriptor.GetConverter(typeof(Geometry));
  36. return (Geometry)converter.ConvertFrom(dataStr);
  37. }
  38. else
  39. {
  40. return "";
  41. }
  42. }
  43. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  44. {
  45. throw new NotImplementedException();
  46. }
  47. }
  48. }