/MahApps.Metro/Controls/RangeSlider.cs
C# | 2439 lines | 2017 code | 233 blank | 189 comment | 391 complexity | 69e258185c7f33d9ebf3c7c05a756a8e MD5 | raw file
Possible License(s): CC-BY-SA-3.0
Large files files are truncated, but you can click here to view the full file
- using System;
- using System.ComponentModel;
- using System.Globalization;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Threading;
- namespace MahApps.Metro.Controls
- {
- public delegate void RangeSelectionChangedEventHandler(object sender, RangeSelectionChangedEventArgs e);
- public delegate void RangeParameterChangedEventHandler(object sender, RangeParameterChangedEventArgs e);
- /// <summary>
- /// A slider control with the ability to select a range between two values.
- /// </summary>
- [DefaultEvent("RangeSelectionChanged"),
- TemplatePart(Name = "PART_Container", Type = typeof(StackPanel)),
- TemplatePart(Name = "PART_RangeSliderContainer", Type = typeof(StackPanel)),
- TemplatePart(Name = "PART_LeftEdge", Type = typeof(RepeatButton)),
- TemplatePart(Name = "PART_RightEdge", Type = typeof(RepeatButton)),
- TemplatePart(Name = "PART_LeftThumb", Type = typeof(Thumb)),
- TemplatePart(Name = "PART_MiddleThumb", Type = typeof(Thumb)),
- TemplatePart(Name = "PART_PART_TopTick", Type = typeof(TickBar)),
- TemplatePart(Name = "PART_PART_BottomTick", Type = typeof(TickBar)),
- TemplatePart(Name = "PART_RightThumb", Type = typeof(Thumb))]
- public class RangeSlider : RangeBase
- {
- #region Routed UI commands
- public static RoutedUICommand MoveBack = new RoutedUICommand("MoveBack", "MoveBack", typeof (RangeSlider),
- new InputGestureCollection(new InputGesture[] {new KeyGesture(Key.B, ModifierKeys.Control)}));
- public static RoutedUICommand MoveForward = new RoutedUICommand("MoveForward", "MoveForward",
- typeof (RangeSlider),
- new InputGestureCollection(new InputGesture[] {new KeyGesture(Key.F, ModifierKeys.Control)}));
- public static RoutedUICommand MoveAllForward = new RoutedUICommand("MoveAllForward", "MoveAllForward",
- typeof (RangeSlider),
- new InputGestureCollection(new InputGesture[] {new KeyGesture(Key.F, ModifierKeys.Alt)}));
- public static RoutedUICommand MoveAllBack = new RoutedUICommand("MoveAllBack", "MoveAllBack",
- typeof (RangeSlider),
- new InputGestureCollection(new InputGesture[] {new KeyGesture(Key.B, ModifierKeys.Alt)}));
- #endregion
- #region Routed events
- public static readonly RoutedEvent RangeSelectionChangedEvent =
- EventManager.RegisterRoutedEvent("RangeSelectionChanged", RoutingStrategy.Bubble,
- typeof (RangeSelectionChangedEventHandler), typeof (RangeSlider));
- public static readonly RoutedEvent LowerValueChangedEvent =
- EventManager.RegisterRoutedEvent("LowerValueChanged", RoutingStrategy.Bubble,
- typeof (RangeParameterChangedEventHandler), typeof (RangeSlider));
- public static readonly RoutedEvent UpperValueChangedEvent =
- EventManager.RegisterRoutedEvent("UpperValueChanged", RoutingStrategy.Bubble,
- typeof (RangeParameterChangedEventHandler), typeof (RangeSlider));
- public static readonly RoutedEvent LowerThumbDragStartedEvent =
- EventManager.RegisterRoutedEvent("LowerThumbDragStarted", RoutingStrategy.Bubble,
- typeof (DragStartedEventHandler), typeof (RangeSlider));
- public static readonly RoutedEvent LowerThumbDragCompletedEvent =
- EventManager.RegisterRoutedEvent("LowerThumbDragCompleted", RoutingStrategy.Bubble,
- typeof (DragCompletedEventHandler), typeof (RangeSlider));
- public static readonly RoutedEvent UpperThumbDragStartedEvent =
- EventManager.RegisterRoutedEvent("UpperThumbDragStarted", RoutingStrategy.Bubble,
- typeof (DragStartedEventHandler), typeof (RangeSlider));
- public static readonly RoutedEvent UpperThumbDragCompletedEvent =
- EventManager.RegisterRoutedEvent("UpperThumbDragCompleted", RoutingStrategy.Bubble,
- typeof (DragCompletedEventHandler), typeof (RangeSlider));
- public static readonly RoutedEvent CentralThumbDragStartedEvent =
- EventManager.RegisterRoutedEvent("CentralThumbDragStarted", RoutingStrategy.Bubble,
- typeof(DragStartedEventHandler), typeof(RangeSlider));
- public static readonly RoutedEvent CentralThumbDragCompletedEvent =
- EventManager.RegisterRoutedEvent("CentralThumbDragCompleted", RoutingStrategy.Bubble,
- typeof(DragCompletedEventHandler), typeof(RangeSlider));
- public static readonly RoutedEvent LowerThumbDragDeltaEvent =
- EventManager.RegisterRoutedEvent("LowerThumbDragDelta", RoutingStrategy.Bubble,
- typeof(DragDeltaEventHandler), typeof(RangeSlider));
- public static readonly RoutedEvent UpperThumbDragDeltaEvent =
- EventManager.RegisterRoutedEvent("UpperThumbDragDelta", RoutingStrategy.Bubble,
- typeof(DragDeltaEventHandler), typeof(RangeSlider));
- public static readonly RoutedEvent CentralThumbDragDeltaEvent =
- EventManager.RegisterRoutedEvent("CentralThumbDragDelta", RoutingStrategy.Bubble,
- typeof(DragDeltaEventHandler), typeof(RangeSlider));
- #endregion
- #region Event handlers
- public event RangeSelectionChangedEventHandler RangeSelectionChanged
- {
- add { AddHandler(RangeSelectionChangedEvent, value); }
- remove { RemoveHandler(RangeSelectionChangedEvent, value); }
- }
- public event RangeParameterChangedEventHandler LowerValueChanged
- {
- add { AddHandler(LowerValueChangedEvent, value); }
- remove { RemoveHandler(LowerValueChangedEvent, value); }
- }
- public event RangeParameterChangedEventHandler UpperValueChanged
- {
- add { AddHandler(UpperValueChangedEvent, value); }
- remove { RemoveHandler(UpperValueChangedEvent, value); }
- }
- public event DragStartedEventHandler LowerThumbDragStarted
- {
- add { AddHandler(LowerThumbDragStartedEvent, value); }
- remove { RemoveHandler(LowerThumbDragStartedEvent, value); }
- }
- public event DragCompletedEventHandler LowerThumbDragCompleted
- {
- add { AddHandler(LowerThumbDragCompletedEvent, value); }
- remove { RemoveHandler(LowerThumbDragCompletedEvent, value); }
- }
- public event DragStartedEventHandler UpperThumbDragStarted
- {
- add { AddHandler(UpperThumbDragStartedEvent, value); }
- remove { RemoveHandler(UpperThumbDragStartedEvent, value); }
- }
- public event DragCompletedEventHandler UpperThumbDragCompleted
- {
- add { AddHandler(UpperThumbDragCompletedEvent, value); }
- remove { RemoveHandler(UpperThumbDragCompletedEvent, value); }
- }
- public event DragStartedEventHandler CentralThumbDragStarted
- {
- add { AddHandler(CentralThumbDragStartedEvent, value); }
- remove { RemoveHandler(CentralThumbDragStartedEvent, value); }
- }
- public event DragCompletedEventHandler CentralThumbDragCompleted
- {
- add { AddHandler(CentralThumbDragCompletedEvent, value); }
- remove { RemoveHandler(CentralThumbDragCompletedEvent, value); }
- }
- public event DragDeltaEventHandler LowerThumbDragDelta
- {
- add { AddHandler(LowerThumbDragDeltaEvent, value); }
- remove { RemoveHandler(LowerThumbDragDeltaEvent, value); }
- }
- public event DragDeltaEventHandler UpperThumbDragDelta
- {
- add { AddHandler(UpperThumbDragDeltaEvent, value); }
- remove { RemoveHandler(UpperThumbDragDeltaEvent, value); }
- }
- public event DragDeltaEventHandler CentralThumbDragDelta
- {
- add { AddHandler(CentralThumbDragDeltaEvent, value); }
- remove { RemoveHandler(CentralThumbDragDeltaEvent, value); }
- }
- #endregion
- #region Dependency properties
- public static readonly DependencyProperty UpperValueProperty =
- DependencyProperty.Register("UpperValue", typeof (Double), typeof (RangeSlider),
- new FrameworkPropertyMetadata((Double) 0,
- FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender, RangesChanged, CoerceUpperValue));
- public static readonly DependencyProperty LowerValueProperty =
- DependencyProperty.Register("LowerValue", typeof(Double), typeof(RangeSlider),
- new FrameworkPropertyMetadata((Double)0,
- FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender, RangesChanged, CoerceLowerValue));
- public static readonly DependencyProperty MinRangeProperty =
- DependencyProperty.Register("MinRange", typeof (Double), typeof (RangeSlider),
- new FrameworkPropertyMetadata((Double)0, MinRangeChanged, CoerceMinRange), IsValidMinRange);
- public static readonly DependencyProperty MinRangeWidthProperty =
- DependencyProperty.Register("MinRangeWidth", typeof(Double), typeof(RangeSlider),
- new FrameworkPropertyMetadata(30.0, MinRangeWidthChanged, CoerceMinRangeWidth), IsValidMinRange);
- public static readonly DependencyProperty MoveWholeRangeProperty =
- DependencyProperty.Register("MoveWholeRange", typeof(Boolean), typeof(RangeSlider),
- new PropertyMetadata(false));
- public static readonly DependencyProperty ExtendedModeProperty =
- DependencyProperty.Register("ExtendedMode", typeof(Boolean), typeof(RangeSlider),
- new PropertyMetadata(false));
- public static readonly DependencyProperty IsSnapToTickEnabledProperty =
- DependencyProperty.Register("IsSnapToTickEnabled", typeof(Boolean), typeof(RangeSlider),
- new PropertyMetadata(false));
- public static readonly DependencyProperty OrientationProperty =
- DependencyProperty.Register("Orientation", typeof(Orientation), typeof(RangeSlider),
- new FrameworkPropertyMetadata(Orientation.Horizontal));
- public static readonly DependencyProperty TickFrequencyProperty =
- DependencyProperty.Register("TickFrequency", typeof(Double), typeof(RangeSlider),
- new FrameworkPropertyMetadata(1.0), IsValidTickFrequency);
- public static readonly DependencyProperty IsMoveToPointEnabledProperty =
- DependencyProperty.Register("IsMoveToPointEnabled", typeof(Boolean), typeof(RangeSlider),
- new PropertyMetadata(false));
- public static readonly DependencyProperty TickPlacementProperty =
- DependencyProperty.Register("TickPlacement", typeof(TickPlacement), typeof(RangeSlider),
- new FrameworkPropertyMetadata(TickPlacement.None));
- public static readonly DependencyProperty AutoToolTipPlacementProperty =
- DependencyProperty.Register("AutoToolTipPlacement", typeof(AutoToolTipPlacement), typeof(RangeSlider),
- new FrameworkPropertyMetadata(AutoToolTipPlacement.None));
- public static readonly DependencyProperty AutoToolTipPrecisionProperty =
- DependencyProperty.Register("AutoToolTipPrecision", typeof (Int32), typeof (RangeSlider),
- new FrameworkPropertyMetadata(0), IsValidPrecision);
- public static readonly DependencyProperty IntervalProperty =
- DependencyProperty.Register("Interval", typeof(Int32), typeof(RangeSlider),
- new FrameworkPropertyMetadata(100, IntervalChangedCallback), IsValidPrecision);
- /// <summary>
- /// Get/sets value how fast thumbs will move when user press on left/right/central with left mouse button (IsMoveToPoint must be set to FALSE)
- /// </summary>
- [Bindable(true), Category("Behavior")]
- public Int32 Interval
- {
- get { return (Int32)GetValue(IntervalProperty); }
- set { SetValue(IntervalProperty, value); }
- }
- /// <summary>
- /// Get/sets precision of the value, which displaying inside AutotToolTip
- /// </summary>
- [Bindable(true), Category("Appearance")]
- public Int32 AutoToolTipPrecision
- {
- get { return (Int32)GetValue(AutoToolTipPrecisionProperty); }
- set { SetValue(AutoToolTipPrecisionProperty, value); }
- }
- /// <summary>
- /// Get/sets tooltip, which will show while dragging thumbs and display currect value
- /// </summary>
- [Bindable(true), Category("Behavior")]
- public AutoToolTipPlacement AutoToolTipPlacement
- {
- get { return (AutoToolTipPlacement)GetValue(AutoToolTipPlacementProperty); }
- set { SetValue(AutoToolTipPlacementProperty, value); }
- }
- /// <summary>
- /// Get/sets tick placement position
- /// </summary>
- [Bindable(true), Category("Common")]
- public TickPlacement TickPlacement
- {
- get { return (TickPlacement)GetValue(TickPlacementProperty); }
- set { SetValue(TickPlacementProperty, value); }
- }
- /// <summary>
- /// Get/sets IsMoveToPoint feature which will enable/disable moving to exact point inside control when user clicked on it
- /// </summary>
- [Bindable(true), Category("Common")]
- public Boolean IsMoveToPointEnabled
- {
- get { return (Boolean)GetValue(IsMoveToPointEnabledProperty); }
- set { SetValue(IsMoveToPointEnabledProperty, value); }
- }
- /// <summary>
- /// Get/sets tickFrequency
- /// </summary>
- [Bindable(true), Category("Common")]
- public Double TickFrequency
- {
- get { return (Double)GetValue(TickFrequencyProperty); }
- set { SetValue(TickFrequencyProperty, value); }
- }
- /// <summary>
- /// Get/sets orientation of range slider
- /// </summary>
- [Bindable(true), Category("Common")]
- public Orientation Orientation
- {
- get { return (Orientation)GetValue(OrientationProperty); }
- set { SetValue(OrientationProperty, value); }
- }
- /// <summary>
- /// Get/sets whether possibility to make manipulations inside range with left/right mouse buttons + cotrol button
- /// </summary>
- [Bindable(true), Category("Appearance")]
- public Boolean IsSnapToTickEnabled
- {
- get { return (Boolean)GetValue(IsSnapToTickEnabledProperty); }
- set { SetValue(IsSnapToTickEnabledProperty, value); }
- }
- /// <summary>
- /// Get/sets whether possibility to make manipulations inside range with left/right mouse buttons + cotrol button
- /// </summary>
- [Bindable(true), Category("Behavior")]
- public Boolean ExtendedMode
- {
- get { return (Boolean)GetValue(ExtendedModeProperty); }
- set { SetValue(ExtendedModeProperty, value); }
- }
- /// <summary>
- /// Get/sets whether whole range will be moved when press on right/left/central part of control
- /// </summary>
- [Bindable(true), Category("Behavior")]
- public Boolean MoveWholeRange
- {
- get { return (Boolean)GetValue(MoveWholeRangeProperty); }
- set { SetValue(MoveWholeRangeProperty, value); }
- }
- /// <summary>
- /// Get/sets the minimal distance between two thumbs.
- /// </summary>
- [Bindable(true), Category("Common")]
- public Double MinRangeWidth
- {
- get { return (Double)GetValue(MinRangeWidthProperty); }
- set { SetValue(MinRangeWidthProperty, value); }
- }
- /// <summary>
- /// Get/sets the beginning of the range selection.
- /// </summary>
- [Bindable(true), Category("Common")]
- public Double LowerValue
- {
- get { return (Double) GetValue(LowerValueProperty); }
- set { SetValue(LowerValueProperty, value); }
- }
- /// <summary>
- /// Get/sets the end of the range selection.
- /// </summary>
- [Bindable(true), Category("Common")]
- public Double UpperValue
- {
- get { return (Double) GetValue(UpperValueProperty); }
- set { SetValue(UpperValueProperty, value); }
- }
- /// <summary>
- /// Get/sets the minimum range that can be selected.
- /// </summary>
- [Bindable(true), Category("Common")]
- public Double MinRange
- {
- get { return (Double) GetValue(MinRangeProperty); }
- set { SetValue(MinRangeProperty, value); }
- }
- #endregion
- #region Variables
- private const double Epsilon = 0.00000153;
- private Boolean _internalUpdate;
- private Thumb _centerThumb;
- private Thumb _leftThumb;
- private Thumb _rightThumb;
- private RepeatButton _leftButton;
- private RepeatButton _rightButton;
- private StackPanel _visualElementsContainer;
- private StackPanel _container;
- private Double _movableWidth;
- private readonly DispatcherTimer _timer;
- private uint _tickCount;
- private Double _currentpoint;
- private Boolean _isInsideRange;
- private Boolean _centerThumbBlocked;
- private Direction _direction;
- private ButtonType _bType;
- private Point _position;
- private Point _basePoint;
- private Double _currenValue;
- private Double _density;
- private ToolTip _autoToolTip;
- Double _oldLower;
- Double _oldUpper;
- private Boolean _isMoved;
- private Boolean _roundToPrecision;
- private Int32 _precision;
- #endregion
- public double MovableRange
- {
- get
- {
- return Maximum - Minimum - MinRange;
- }
- }
- public RangeSlider()
- {
- CommandBindings.Add(new CommandBinding(MoveBack, MoveBackHandler));
- CommandBindings.Add(new CommandBinding(MoveForward, MoveForwardHandler));
- CommandBindings.Add(new CommandBinding(MoveAllForward, MoveAllForwardHandler));
- CommandBindings.Add(new CommandBinding(MoveAllBack, MoveAllBackHandler));
-
- DependencyPropertyDescriptor.FromProperty(ActualWidthProperty, typeof(RangeSlider)).AddValueChanged(this, delegate { ReCalculateSize(); });
- DependencyPropertyDescriptor.FromProperty(ActualHeightProperty, typeof(RangeSlider)).AddValueChanged(this, delegate { ReCalculateSize(); });
- _timer = new DispatcherTimer();
- _timer.Tick += MoveToNextValue;
- _timer.Interval = TimeSpan.FromMilliseconds(Interval);
- }
- static RangeSlider()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(RangeSlider), new FrameworkPropertyMetadata(typeof(RangeSlider)));
- MinimumProperty.OverrideMetadata(typeof(RangeSlider), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsMeasure, MinPropertyChangedCallback, CoerceMinimum));
- MaximumProperty.OverrideMetadata(typeof(RangeSlider), new FrameworkPropertyMetadata(100.0, FrameworkPropertyMetadataOptions.AffectsMeasure, MaxPropertyChangedCallback, CoerceMaximum));
- }
- /// <summary>
- /// Responds to a change in the value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Minimum"/> property.
- /// </summary>
- /// <param name="oldMinimum">The old value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Minimum"/> property.</param><param name="newMinimum">The new value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Minimum"/> property.</param>
- protected override void OnMinimumChanged(double oldMinimum, double newMinimum)
- {
- ReCalculateSize();
- }
- /// <summary>
- /// Responds to a change in the value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Maximum"/> property.
- /// </summary>
- /// <param name="oldMaximum">The old value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Maximum"/> property.</param><param name="newMaximum">The new value of the <see cref="P:System.Windows.Controls.Primitives.RangeBase.Maximum"/> property.</param>
- protected override void OnMaximumChanged(double oldMaximum, double newMaximum)
- {
- ReCalculateSize();
- }
- void MoveAllBackHandler(object sender, ExecutedRoutedEventArgs e)
- {
- ResetSelection(true);
- }
- void MoveAllForwardHandler(object sender, ExecutedRoutedEventArgs e)
- {
- ResetSelection(false);
- }
- void MoveBackHandler(object sender, ExecutedRoutedEventArgs e)
- {
- MoveSelection(true);
- }
- void MoveForwardHandler(object sender, ExecutedRoutedEventArgs e)
- {
- MoveSelection(false);
- }
- private static void MoveThumb(FrameworkElement x, FrameworkElement y, double horizonalChange,
- Orientation orientation)
- {
- double change;
- if (orientation == Orientation.Horizontal)
- {
- if (!Double.IsNaN(x.Width) && !Double.IsNaN(y.Width))
- {
- if (horizonalChange < 0) //slider went left
- {
- change = GetChangeKeepPositive(x.Width, horizonalChange);
- if (x.Name == "PART_MiddleThumb")
- {
- if (x.Width > x.MinWidth)
- {
- if (x.Width + change < x.MinWidth)
- {
- double dif = x.Width - x.MinWidth;
- x.Width = x.MinWidth;
- y.Width += dif;
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- else if (horizonalChange > 0) //slider went right if(horizontal change == 0 do nothing)
- {
- change = -GetChangeKeepPositive(y.Width, -horizonalChange);
- if (y.Name == "PART_MiddleThumb")
- {
- if (y.Width > y.MinWidth)
- {
- if (y.Width - change < y.MinWidth)
- {
- double dif = y.Width - y.MinWidth;
- y.Width = y.MinWidth;
- x.Width += dif;
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- }
- }
- else if (orientation == Orientation.Vertical)
- {
- if (!Double.IsNaN(x.Height) && !Double.IsNaN(y.Height))
- {
- if (horizonalChange < 0) //slider went up
- {
- change = -GetChangeKeepPositive(y.Height, horizonalChange);//get positive number
- if (y.Name == "PART_MiddleThumb")
- {
- if (y.Height > y.MinHeight)
- {
- if (y.Height - change < y.MinHeight)
- {
- double dif = y.Height - y.MinHeight;
- y.Height = y.MinHeight;
- x.Height += dif;
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- else if (horizonalChange > 0) //slider went down if(horizontal change == 0 do nothing)
- {
- change = GetChangeKeepPositive(x.Height, -horizonalChange);//get negative number
- if (x.Name == "PART_MiddleThumb")
- {
- if (x.Height > y.MinHeight)
- {
- if (x.Height + change < x.MinHeight)
- {
- double dif = x.Height - x.MinHeight;
- x.Height = x.MinHeight;
- y.Height += dif;
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- }
- }
- }
- private static void MoveThumb(FrameworkElement x, FrameworkElement y, double horizonalChange,
- Orientation orientation, out Direction direction)
- {
- double change;
- direction = Direction.Increase;
- if (orientation == Orientation.Horizontal)
- {
- if (!Double.IsNaN(x.Width) && !Double.IsNaN(y.Width))
- {
- if (horizonalChange < 0) //slider went left
- {
- direction = Direction.Decrease;
- change = GetChangeKeepPositive(x.Width, horizonalChange);
- if (x.Name == "PART_MiddleThumb")
- {
- if (x.Width > x.MinWidth)
- {
- if (x.Width + change < x.MinWidth)
- {
- double dif = x.Width - x.MinWidth;
- x.Width = x.MinWidth;
- y.Width += dif;
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- else if (horizonalChange > 0) //slider went right if(horizontal change == 0 do nothing)
- {
- direction = Direction.Increase;
- change = -GetChangeKeepPositive(y.Width, -horizonalChange);
- if (y.Name == "PART_MiddleThumb")
- {
- if (y.Width > y.MinWidth)
- {
- if (y.Width - change < y.MinWidth)
- {
- double dif = y.Width - y.MinWidth;
- y.Width = y.MinWidth;
- x.Width += dif;
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- }
- else
- {
- x.Width += change;
- y.Width -= change;
- }
- }
- }
- }
- else
- {
- if (!Double.IsNaN(x.Height) && !Double.IsNaN(y.Height))
- {
- if (horizonalChange < 0) //slider went up
- {
- direction = Direction.Increase;
- change = -GetChangeKeepPositive(y.Height, horizonalChange);//get positive number
- if (y.Name == "PART_MiddleThumb")
- {
- if (y.Height > y.MinHeight)
- {
- if (y.Height - change < y.MinHeight)
- {
- double dif = y.Height - y.MinHeight;
- y.Height = y.MinHeight;
- x.Height += dif;
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- else if (horizonalChange > 0) //slider went down if(horizontal change == 0 do nothing)
- {
- direction = Direction.Decrease;
- change = GetChangeKeepPositive(x.Height, -horizonalChange);//get negative number
- if (x.Name == "PART_MiddleThumb")
- {
- if (x.Height > y.MinHeight)
- {
- if (x.Height + change < x.MinHeight)
- {
- double dif = x.Height - x.MinHeight;
- x.Height = x.MinHeight;
- y.Height += dif;
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- }
- else
- {
- x.Height += change;
- y.Height -= change;
- }
- }
- }
- }
-
- }
-
- //Recalculation of Control Height or Width
- private void ReCalculateSize()
- {
- if (_leftButton != null && _rightButton != null && _centerThumb != null)
- {
- if (Orientation == Orientation.Horizontal)
- {
- _movableWidth =
- Math.Max(
- ActualWidth - _rightThumb.ActualWidth - _leftThumb.ActualWidth - MinRangeWidth, 1);
- if (MovableRange <= 0)
- {
- _leftButton.Width = Double.NaN;
- _rightButton.Width = Double.NaN;
- }
- else
- {
- _leftButton.Width = Math.Max(_movableWidth * (LowerValue - Minimum) / MovableRange, 0);
- _rightButton.Width = Math.Max(_movableWidth * (Maximum - UpperValue) / MovableRange, 0);
- }
-
- if (IsValidDouble(_rightButton.Width) && IsValidDouble(_leftButton.Width))
- {
- _centerThumb.Width =
- Math.Max(
- ActualWidth - (_leftButton.Width + _rightButton.Width + _rightThumb.ActualWidth +
- _leftThumb.ActualWidth), 0);
- }
- else
- {
- _centerThumb.Width =
- Math.Max(
- ActualWidth - (_rightThumb.ActualWidth + _leftThumb.ActualWidth), 0);
- }
- }
- else if (Orientation == Orientation.Vertical)
- {
- _movableWidth =
- Math.Max(
- ActualHeight - _rightThumb.ActualHeight - _leftThumb.ActualHeight - MinRangeWidth, 1);
- if (MovableRange <= 0)
- {
- _leftButton.Height = Double.NaN;
- _rightButton.Height = Double.NaN;
- }
- else
- {
- _leftButton.Height = Math.Max(_movableWidth * (LowerValue - Minimum) / MovableRange, 0);
- _rightButton.Height = Math.Max(_movableWidth * (Maximum - UpperValue) / MovableRange, 0);
- }
-
- if (IsValidDouble(_rightButton.Height) && IsValidDouble(_leftButton.Height))
- {
- _centerThumb.Height =
- Math.Max(
- ActualHeight - (_leftButton.Height + _rightButton.Height + _rightThumb.ActualHeight +
- _leftThumb.ActualHeight), 0);
- }
- else
- {
- _centerThumb.Height =
- Math.Max(
- ActualHeight - (_rightThumb.ActualHeight + _leftThumb.ActualHeight), 0);
- }
- }
- _density = _movableWidth / MovableRange;
- }
-
- }
-
- /*
- //private void ReCalculateRangeSelected(bool reCalculateLowerValue, bool reCalculateUpperValue)
- //{
- // _internalUpdate = true; //set flag to signal that the properties are being set by the object itself
- // if (reCalculateLowerValue)
- // {
- // _oldLower = LowerValue;
- // double width = Orientation == Orientation.Horizontal ? _leftButton.Width : _leftButton.Height;
- // //Check first if button width is not Double.NaN
- // if (IsValidDouble(width))
- // {
- // // Make sure to get exactly rangestart if thumb is at the start
- // double lower = Equals(width, 0.0)
- // ? Minimum
- // : Math.Max(Minimum, (Minimum + MovableRange * width / _movableWidth));
- // if (!_isMoved)
- // {
- // LowerValue = _roundToPrecision ? Math.Round(lower, _precision) : lower;
- // }
- // else
- // {
- // LowerValue = lower;
- // }
- // }
- // }
- // if (reCalculateUpperValue)
- // {
- // _oldUpper = UpperValue;
- // double width = Orientation == Orientation.Horizontal ? _rightButton.Width : _rightButton.Height;
- // //Check first if button width is not Double.NaN
- // if (IsValidDouble(width))
- // {
- // // Make sure to get exactly rangestop if thumb is at the end
- // double upper = Equals(width, 0.0)
- // ? Maximum
- // : Math.Min(Maximum, (Maximum - MovableRange * width / _movableWidth));
- // if (!_isMoved)
- // {
- // UpperValue = _roundToPrecision
- // ? Math.Round(upper, _precision)
- // : upper;
- // }
- // else
- // {
- // UpperValue = upper;
- // }
- // }
- // }
- // _roundToPrecision = false;
- // _internalUpdate = false; //set flag to signal that the properties are being set by the object itself
- // if (reCalculateLowerValue || reCalculateUpperValue)
- // {
- // if (!Equals(_oldLower, LowerValue) || !Equals(_oldUpper, UpperValue))
- // {
- // //raise the RangeSelectionChanged event
- // OnRangeSelectionChanged(new RangeSelectionChangedEventArgs(LowerValue, UpperValue, _oldLower,
- // _oldUpper));
- // }
- // }
- // if (reCalculateLowerValue && !Equals(_oldLower, LowerValue))
- // {
- // OnRangeParameterChanged(
- // new RangeParameterChangedEventArgs(RangeParameterChangeType.Lower, _oldLower, LowerValue),
- // LowerValueChangedEvent);
- // }
- // if (reCalculateUpperValue && !Equals(_oldUpper, UpperValue))
- // {
- // OnRangeParameterChanged(
- // new RangeParameterChangedEventArgs(RangeParameterChangeType.Upper, _oldUpper, UpperValue),
- // UpperValueChangedEvent);
- // }
- //}
- */
-
- //Method calculates new values when IsSnapToTickEnabled = FALSE
- private void ReCalculateRangeSelected(bool reCalculateLowerValue, bool reCalculateUpperValue, Direction direction)
- {
- _internalUpdate = true; //set flag to signal that the properties are being set by the object itself
- if (direction == Direction.Increase)
- {
- if (reCalculateUpperValue)
- {
- _oldUpper = UpperValue;
- double width = Orientation == Orientation.Horizontal ? _rightButton.Width : _rightButton.Height;
- //Check first if button width is not Double.NaN
- if (IsValidDouble(width))
- {
- // Make sure to get exactly rangestop if thumb is at the end
- double upper = Equals(width, 0.0)
- ? Maximum
- : Math.Min(Maximum, (Maximum - MovableRange * width / _movableWidth));
- if (!_isMoved)
- {
- UpperValue = _roundToPrecision
- ? Math.Round(upper, _precision)
- : upper;
- }
- else
- {
- UpperValue = upper;
- }
- }
- }
- if (reCalculateLowerValue)
- {
- _oldLower = LowerValue;
- double width = Orientation == Orientation.Horizontal ? _leftButton.Width : _leftButton.Height;
- //Check first if button width is not Double.NaN
- if (IsValidDouble(width))
- {
- // Make sure to get exactly rangestart if thumb is at the start
- double lower = Equals(width, 0.0)
- ? Minimum
- : Math.Max(Minimum, (Minimum + MovableRange*width/_movableWidth));
- if (!_isMoved)
- {
- LowerValue = _roundToPrecision ? Math.Round(lower, _precision) : lower;
- }
- else
- {
- LowerValue = lower;
- }
- }
- }
-
- }
- else
- {
- if (reCalculateLowerValue)
- {
- _oldLower = LowerValue;
- double width = Orientation == Orientation.Horizontal ? _leftButton.Width : _leftButton.Height;
- //Check first if button width is not Double.NaN
- if (IsValidDouble(width))
- {
- // Make sure to get exactly rangestart if thumb is at the start
- double lower = Equals(width, 0.0)
- ? Minimum
- : Math.Max(Minimum, (Minimum + MovableRange * width / _movableWidth));
- if (!_isMoved)
- {
- LowerValue = _roundToPrecision ? Math.Round(lower, _precision) : lower;
- }
- else
- {
- LowerValue = lower;
- }
- }
- }
- if (reCalculateUpperValue)
- {
- _oldUpper = UpperValue;
- double width = Orientation == Orientation.Horizontal ? _rightButton.Width : _rightButton.Height;
- //Check first if button width is not Double.NaN
- if (IsValidDouble(width))
- {
- // Make sure to get exactly rangestop if thumb is at the end
- double upper = Equals(width, 0.0)
- ? Maximum
- : Math.Min(Maximum, (Maximum - MovableRange * width / _movableWidth));
- if (!_isMoved)
- {
- UpperValue = _roundToPrecision
- ? Math.Round(upper, _precision)
- : upper;
- }
- else
- {
- UpperValue = upper;
- }
- }
- }
-
- }
- _roundToPrecision = false;
- _internalUpdate = false; //set flag to signal that the properties are being set by the object itself
- if (reCalculateLowerValue || reCalculateUpperValue)
- {
- if (!Equals(_oldLower, LowerValue) || !Equals(_oldUpper, UpperValue))
- {
- //raise the RangeSelectionChanged event
- OnRangeSelectionChanged(new RangeSelectionChangedEventArgs(LowerValue, UpperValue, _oldLower,
- _oldUpper));
- }
- }
- if (reCalculateLowerValue && !Equals(_oldLower, LowerValue))
- {
- OnRangeParameterChanged(
- new RangeParameterChangedEventArgs(RangeParameterChangeType.Lower, _oldLower, LowerValue),
- LowerValueChangedEvent);
- }
- if (reCalculateUpperValue && !Equals(_oldUpper, UpperValue))
- {
- OnRangeParameterChanged(
- new RangeParameterChangedEventArgs(RangeParameterChangeType.Upper, _oldUpper, UpperValue),
- UpperValueChangedEvent);
- }
- }
- //Method used for cheking and setting correct values when IsSnapToTickEnable = TRUE (When thumb moving separately)
- private void ReCalculateRangeSelected(bool reCalculateLowerValue, bool reCalculateUpperValue, double value, Direction direction)
- {
- _internalUpdate = true; //set flag to signal that the properties are being set by the object itself
- if (reCalculateLowerValue)
- {
- _oldLower = LowerValue;
- double lower = 0;
- if (IsSnapToTickEnabled)
- {
- if (direction == Direction.Increase)
- {
- lower = Math.Min(UpperValue - MinRange, value);
- }
- else
- {
- lower = Math.Max(Minimum, value);
- }
- }
- if (!TickFrequency.ToString(CultureInfo.InvariantCulture).ToLower().Contains("e+") &&
- TickFrequency.ToString(CultureInfo.InvariantCulture).Contains("."))
- {
- //decimal part is for cutting value exactly on that number of digits, which has TickFrequency to have correct values
- String[] decimalPart = TickFrequency.ToString(CultureInfo.InvariantCulture).Split('.');
- LowerValue = Math.Round(lower, decimalPart[1].Length, MidpointRounding.AwayFromZero);
- }
- else
- {
- LowerValue = lower;
- }
- }
- if (reCalculateUpperValue)
- {
- _oldUpper = UpperValue;
- double upper = 0;
- if (IsSnapToTickEnabled)
- {
- if (direction == Direction.Increase)
- {
- upper = Math.Min(value, Maximum);
- }
- else
- {
- upper = Math.Max(LowerValue + MinRange, value);
- }
- }
- if (!TickFrequency.ToString(CultureInfo.InvariantCulture).ToLower().Contains("e+") &&
- TickFrequency.ToString(CultureInfo.InvariantCulture).Contains("."))
- {
- String[] decimalPart = TickFrequency.ToString(CultureInfo.InvariantCulture).Split('.');
- UpperValue = Math.Round(upper, decimalPart[1].Length, MidpointRounding.AwayFromZero);
- }
- else
- {
- UpperValue = upper;
- }
- }
- _internalUpdate = false; //set flag to signal that the properties are being set by the object itself
- if (reCalculateLowerValue || reCalculateUpperValue)
- {
- if (!Equals(_oldLower, LowerValue) || !Equals(_oldUpper, UpperValue))
- {
- //raise the RangeSelectionChanged event
- OnRangeSelectionChanged(new RangeSelectionChangedEventArgs(LowerValue, UpperValue, _oldLower,
- _oldUpper));
- }
- }
- if (reCalculateLowerValue && !Equals(_oldLower, LowerValue))
- {
- OnRangeParameterChanged(
- new RangeParameterChangedEventArgs(RangeParameterChangeType.Lower, _oldLower, LowerValue),
- LowerValueChangedEvent);
- }
- if (reCalculateUpperValue && !Equals(_oldUpper, UpperValue))
- {
- OnRangeParameterChanged(
- new RangeParameterChangedEventArgs(RangeParameterChangeType.Upper, _oldUpper, UpperValue),
- UpperValueChangedEvent);
- }
- }
- //Method used for cheking and setting correct values when IsSnapToTickEnable = TRUE (When thumb moving together)
- private void ReCalculateRangeSelected(double newLower, double newUpper, Direction direction)
- {
- double lower = 0, upper = 0;
- _internalUpdate = true; //set flag to signal that the properties are being set by the object itself
- _oldLower = LowerValue;
- _oldUpper = UpperValue;
-
- if (IsSnapToTickEnabled)
- {
- if (direction == Direction.Increase)
- {
- lower = Math.Min(newLower, Maximum-(UpperValue - LowerValue));
- upper = Math.Min(newUpper, Maximum);
- }
- else
- {
- lower = Math.Max(newLower, Minimum);
- upper = Math.Max(Minimum+(UpperValue - LowerValue), newUpper);
- }
- if (!TickFrequency.ToString(CultureInfo.InvariantCulture).ToLower().Contains("e+") &&
- TickFrequency.ToString(CultureInfo.InvariantCulture).Contains("."))
- {
- //decimal part is for cutting value exactly on that number of digits, which has TickFrequency to have correct values
- String[] decimalPart = TickFrequency.ToString(CultureInfo.InvariantCulture).Split('.');
- //used when whole range decreasing to have correct updated values (lower first, upper - second)
- if (direction == Direction.Decrease)
- {
- LowerValue = Math.Round(lower, decimalPart[1].Length, …
Large files files are truncated, but you can click here to view the full file