CircleProgressBar.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Controls.Primitives;
  4. using HandyControl.Data;
  5. using HandyControl.Expression.Shapes;
  6. namespace HandyControl.Controls;
  7. [TemplatePart(Name = IndicatorTemplateName, Type = typeof(Arc))]
  8. public class CircleProgressBar : RangeBase
  9. {
  10. private const string IndicatorTemplateName = "PART_Indicator";
  11. public static readonly DependencyProperty ArcThicknessProperty = DependencyProperty.Register(
  12. nameof(ArcThickness), typeof(double), typeof(CircleProgressBar), new PropertyMetadata(ValueBoxes.Double0Box));
  13. public static readonly DependencyProperty ShowTextProperty = DependencyProperty.Register(
  14. nameof(ShowText), typeof(bool), typeof(CircleProgressBar), new PropertyMetadata(ValueBoxes.TrueBox));
  15. public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
  16. nameof(Text), typeof(string), typeof(CircleProgressBar), new PropertyMetadata(default(string)));
  17. public static readonly DependencyProperty IsIndeterminateProperty =
  18. ProgressBar.IsIndeterminateProperty.AddOwner(typeof(CircleProgressBar),
  19. new FrameworkPropertyMetadata(ValueBoxes.FalseBox));
  20. private Arc _indicator;
  21. static CircleProgressBar()
  22. {
  23. FocusableProperty.OverrideMetadata(typeof(CircleProgressBar),
  24. new FrameworkPropertyMetadata(ValueBoxes.FalseBox));
  25. MaximumProperty.OverrideMetadata(typeof(CircleProgressBar),
  26. new FrameworkPropertyMetadata(ValueBoxes.Double100Box));
  27. }
  28. public string Text
  29. {
  30. get => (string) GetValue(TextProperty);
  31. set => SetValue(TextProperty, value);
  32. }
  33. public bool ShowText
  34. {
  35. get => (bool) GetValue(ShowTextProperty);
  36. set => SetValue(ShowTextProperty, ValueBoxes.BooleanBox(value));
  37. }
  38. public double ArcThickness
  39. {
  40. get => (double) GetValue(ArcThicknessProperty);
  41. set => SetValue(ArcThicknessProperty, value);
  42. }
  43. public bool IsIndeterminate
  44. {
  45. get => (bool) GetValue(IsIndeterminateProperty);
  46. set => SetValue(IsIndeterminateProperty, ValueBoxes.BooleanBox(value));
  47. }
  48. private void SetProgressBarIndicatorAngle()
  49. {
  50. if (_indicator == null) return;
  51. var minimum = Minimum;
  52. var maximum = Maximum;
  53. var num = Value;
  54. _indicator.EndAngle = (maximum <= minimum ? 0 : (num - minimum) / (maximum - minimum)) * 360;
  55. }
  56. public override void OnApplyTemplate()
  57. {
  58. base.OnApplyTemplate();
  59. _indicator = GetTemplateChild(IndicatorTemplateName) as Arc;
  60. if (_indicator != null)
  61. {
  62. _indicator.StartAngle = 0;
  63. _indicator.EndAngle = 0;
  64. }
  65. SetProgressBarIndicatorAngle();
  66. }
  67. protected override void OnMinimumChanged(double oldMinimum, double newMinimum)
  68. {
  69. base.OnMinimumChanged(oldMinimum, newMinimum);
  70. SetProgressBarIndicatorAngle();
  71. }
  72. protected override void OnMaximumChanged(double oldMaximum, double newMaximum)
  73. {
  74. base.OnMaximumChanged(oldMaximum, newMaximum);
  75. SetProgressBarIndicatorAngle();
  76. }
  77. protected override void OnValueChanged(double oldValue, double newValue)
  78. {
  79. base.OnValueChanged(oldValue, newValue);
  80. SetProgressBarIndicatorAngle();
  81. }
  82. }