TwoWayRangeBase.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Adapted from https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/Primitives/RangeBase.cs
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using HandyControl.Data;
  5. using HandyControl.Tools;
  6. namespace HandyControl.Controls;
  7. public class TwoWayRangeBase : Control
  8. {
  9. public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register(
  10. nameof(Minimum), typeof(double), typeof(TwoWayRangeBase),
  11. new PropertyMetadata(ValueBoxes.Double0Box, OnMinimumChanged), ValidateHelper.IsInRangeOfDouble);
  12. private static void OnMinimumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  13. {
  14. var ctrl = (TwoWayRangeBase) d;
  15. ctrl.CoerceValue(MaximumProperty);
  16. ctrl.CoerceValue(ValueStartProperty);
  17. ctrl.CoerceValue(ValueEndProperty);
  18. ctrl.OnMinimumChanged((double) e.OldValue, (double) e.NewValue);
  19. }
  20. protected virtual void OnMinimumChanged(double oldMinimum, double newMinimum)
  21. {
  22. }
  23. public double Minimum
  24. {
  25. get => (double) GetValue(MinimumProperty);
  26. set => SetValue(MinimumProperty, value);
  27. }
  28. public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
  29. nameof(Maximum), typeof(double), typeof(TwoWayRangeBase),
  30. new PropertyMetadata(ValueBoxes.Double10Box, OnMaximumChanged, CoerceMaximum),
  31. ValidateHelper.IsInRangeOfDouble);
  32. private static object CoerceMaximum(DependencyObject d, object basevalue)
  33. {
  34. var ctrl = (TwoWayRangeBase) d;
  35. var min = ctrl.Minimum;
  36. if ((double) basevalue < min)
  37. {
  38. return min;
  39. }
  40. return basevalue;
  41. }
  42. private static void OnMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  43. {
  44. var ctrl = (TwoWayRangeBase) d;
  45. ctrl.CoerceValue(ValueStartProperty);
  46. ctrl.CoerceValue(ValueEndProperty);
  47. ctrl.OnMaximumChanged((double) e.OldValue, (double) e.NewValue);
  48. }
  49. protected virtual void OnMaximumChanged(double oldMaximum, double newMaximum)
  50. {
  51. }
  52. public double Maximum
  53. {
  54. get => (double) GetValue(MaximumProperty);
  55. set => SetValue(MaximumProperty, value);
  56. }
  57. public static readonly DependencyProperty ValueStartProperty = DependencyProperty.Register(
  58. nameof(ValueStart), typeof(double), typeof(TwoWayRangeBase),
  59. new FrameworkPropertyMetadata(ValueBoxes.Double0Box,
  60. FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
  61. OnValueStartChanged, ConstrainToRange), ValidateHelper.IsInRangeOfDouble);
  62. private static void OnValueStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  63. {
  64. var ctrl = (TwoWayRangeBase) d;
  65. ctrl.OnValueChanged(new DoubleRange
  66. {
  67. Start = (double) e.OldValue,
  68. End = ctrl.ValueEnd
  69. }, new DoubleRange
  70. {
  71. Start = (double) e.NewValue,
  72. End = ctrl.ValueEnd
  73. });
  74. }
  75. protected virtual void OnValueChanged(DoubleRange oldValue, DoubleRange newValue) => RaiseEvent(
  76. new RoutedPropertyChangedEventArgs<DoubleRange>(oldValue, newValue) { RoutedEvent = ValueChangedEvent });
  77. public double ValueStart
  78. {
  79. get => (double) GetValue(ValueStartProperty);
  80. set => SetValue(ValueStartProperty, value);
  81. }
  82. public static readonly DependencyProperty ValueEndProperty = DependencyProperty.Register(
  83. nameof(ValueEnd), typeof(double), typeof(TwoWayRangeBase),
  84. new FrameworkPropertyMetadata(ValueBoxes.Double0Box,
  85. FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
  86. OnValueEndChanged, ConstrainToRange), ValidateHelper.IsInRangeOfDouble);
  87. private static void OnValueEndChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  88. {
  89. var ctrl = (TwoWayRangeBase) d;
  90. ctrl.OnValueChanged(new DoubleRange
  91. {
  92. Start = ctrl.ValueStart,
  93. End = (double) e.OldValue
  94. }, new DoubleRange
  95. {
  96. Start = ctrl.ValueStart,
  97. End = (double) e.NewValue
  98. });
  99. }
  100. public double ValueEnd
  101. {
  102. get => (double) GetValue(ValueEndProperty);
  103. set => SetValue(ValueEndProperty, value);
  104. }
  105. internal static object ConstrainToRange(DependencyObject d, object value)
  106. {
  107. var ctrl = (TwoWayRangeBase) d;
  108. var min = ctrl.Minimum;
  109. var v = (double) value;
  110. if (v < min)
  111. {
  112. return min;
  113. }
  114. var max = ctrl.Maximum;
  115. if (v > max)
  116. {
  117. return max;
  118. }
  119. return value;
  120. }
  121. public static readonly DependencyProperty LargeChangeProperty = DependencyProperty.Register(
  122. nameof(LargeChange), typeof(double), typeof(TwoWayRangeBase), new PropertyMetadata(ValueBoxes.Double1Box),
  123. ValidateHelper.IsInRangeOfPosDoubleIncludeZero);
  124. public double LargeChange
  125. {
  126. get => (double) GetValue(LargeChangeProperty);
  127. set => SetValue(LargeChangeProperty, value);
  128. }
  129. public static readonly DependencyProperty SmallChangeProperty = DependencyProperty.Register(
  130. nameof(SmallChange), typeof(double), typeof(TwoWayRangeBase), new PropertyMetadata(ValueBoxes.Double01Box),
  131. ValidateHelper.IsInRangeOfPosDoubleIncludeZero);
  132. public double SmallChange
  133. {
  134. get => (double) GetValue(SmallChangeProperty);
  135. set => SetValue(SmallChangeProperty, value);
  136. }
  137. public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged",
  138. RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<DoubleRange>), typeof(TwoWayRangeBase));
  139. public event RoutedPropertyChangedEventHandler<DoubleRange> ValueChanged
  140. {
  141. add => AddHandler(ValueChangedEvent, value);
  142. remove => RemoveHandler(ValueChangedEvent, value);
  143. }
  144. }