PageRenderTime 1480ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Xceed.Wpf.Toolkit/RangeSlider/Implementation/RangeSlider.cs

https://github.com/xyshyniaphy/TestRx
C# | 803 lines | 573 code | 139 blank | 91 comment | 57 complexity | eafc967f5ef5c206c5b87eb6eb96911a MD5 | raw file
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Controls;
  15. using System.Windows;
  16. using System.Windows.Controls.Primitives;
  17. using System.ComponentModel;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. namespace Xceed.Wpf.Toolkit
  21. {
  22. [TemplatePart(Name = PART_LowerRange, Type = typeof(RepeatButton))]
  23. [TemplatePart(Name = PART_HigherRange, Type = typeof(RepeatButton))]
  24. [TemplatePart( Name = PART_HigherSlider, Type = typeof( Slider ) )]
  25. [TemplatePart( Name = PART_LowerSlider, Type = typeof( Slider ) )]
  26. public class RangeSlider : Control
  27. {
  28. #region Members
  29. private const String PART_LowerRange = "PART_LowerRange";
  30. private const String PART_Range = "PART_Range";
  31. private const String PART_HigherRange = "PART_HigherRange";
  32. private const String PART_HigherSlider = "PART_HigherSlider";
  33. private const String PART_LowerSlider = "PART_LowerSlider";
  34. private RepeatButton _lowerRange;
  35. private RepeatButton _higherRange;
  36. private Slider _lowerSlider;
  37. private Slider _higherSlider;
  38. #endregion Members
  39. #region Enums
  40. public enum OrientationEnum
  41. {
  42. Horizontal,
  43. Vertical
  44. };
  45. #endregion Enums
  46. #region Constructors
  47. static RangeSlider()
  48. {
  49. DefaultStyleKeyProperty.OverrideMetadata(typeof(RangeSlider), new FrameworkPropertyMetadata(typeof(RangeSlider)));
  50. }
  51. public RangeSlider()
  52. {
  53. this.SizeChanged += this.RangeSlider_SizeChanged;
  54. }
  55. #endregion Constructors
  56. #region Properties
  57. #region HigherRangeBackground
  58. /// <summary>
  59. /// # TODODOC
  60. /// Get/Set the Brush for the Range between higher/maximum values. (Brush)
  61. /// </summary>
  62. public static readonly DependencyProperty HigherRangeBackgroundProperty = DependencyProperty.Register( "HigherRangeBackground", typeof( Brush ), typeof( RangeSlider ) );
  63. public Brush HigherRangeBackground
  64. {
  65. get
  66. {
  67. return ( Brush )GetValue( RangeSlider.HigherRangeBackgroundProperty );
  68. }
  69. set
  70. {
  71. SetValue( RangeSlider.HigherRangeBackgroundProperty, value );
  72. }
  73. }
  74. #endregion HigherRangeBackground
  75. #region HigherRangeStyle
  76. /// <summary>
  77. /// # TODODOC
  78. /// Get/Set the Style for the Range between Higher/Maximum values. (Style)
  79. /// </summary>
  80. public static readonly DependencyProperty HigherRangeStyleProperty = DependencyProperty.Register( "HigherRangeStyle", typeof( Style ), typeof( RangeSlider )
  81. , new FrameworkPropertyMetadata( null ) );
  82. public Style HigherRangeStyle
  83. {
  84. get
  85. {
  86. return ( Style )this.GetValue( RangeSlider.HigherRangeStyleProperty );
  87. }
  88. set
  89. {
  90. this.SetValue( RangeSlider.HigherRangeStyleProperty, value );
  91. }
  92. }
  93. #endregion HigherRangeStyle
  94. #region HigherRangeWidth
  95. /// <summary>
  96. /// # TODODOC
  97. ///
  98. /// HigherRangeWidth property is a readonly property, used to calculate the percentage of the HigherRange within the entire min/max range.
  99. /// </summary>
  100. ///
  101. private static readonly DependencyPropertyKey HigherRangeWidthPropertyKey = DependencyProperty.RegisterAttachedReadOnly( "HigherRangeWidth", typeof( double )
  102. , typeof( RangeSlider ), new PropertyMetadata( 0d ) );
  103. public static readonly DependencyProperty HigherRangeWidthProperty = HigherRangeWidthPropertyKey.DependencyProperty;
  104. public double HigherRangeWidth
  105. {
  106. get
  107. {
  108. return ( double )GetValue( RangeSlider.HigherRangeWidthProperty );
  109. }
  110. private set
  111. {
  112. SetValue( RangeSlider.HigherRangeWidthPropertyKey, value );
  113. }
  114. }
  115. #endregion HigherRangeWidth
  116. #region HigherThumbBackground
  117. /// <summary>
  118. /// # TODODOC
  119. /// Get/Set the Brush for the HigherValue thumb back of the icons [active state]. (Brush)
  120. /// </summary>
  121. public static readonly DependencyProperty HigherThumbBackgroundProperty = DependencyProperty.Register( "HigherThumbBackground", typeof( Brush ), typeof( RangeSlider ));
  122. public Brush HigherThumbBackground
  123. {
  124. get
  125. {
  126. return ( Brush )GetValue( RangeSlider.HigherThumbBackgroundProperty );
  127. }
  128. set
  129. {
  130. SetValue( RangeSlider.HigherThumbBackgroundProperty, value );
  131. }
  132. }
  133. #endregion HigherThumbBackground
  134. #region HigherValue
  135. /// <summary>
  136. /// # TODODOC
  137. ///
  138. /// HigherValue property represents the higher value within the selected range.
  139. /// </summary>
  140. public static readonly DependencyProperty HigherValueProperty = DependencyProperty.Register( "HigherValue", typeof( double ), typeof( RangeSlider )
  141. , new FrameworkPropertyMetadata( RangeSlider.OnHigherValueChanged, RangeSlider.CoerceHigherValue ) );
  142. public double HigherValue
  143. {
  144. get
  145. {
  146. return ( double )GetValue( RangeSlider.HigherValueProperty );
  147. }
  148. set
  149. {
  150. SetValue( RangeSlider.HigherValueProperty, value );
  151. }
  152. }
  153. private static object CoerceHigherValue( DependencyObject sender, object value )
  154. {
  155. RangeSlider rangeSlider = sender as RangeSlider;
  156. double newValue = ( double )value;
  157. return Math.Max( rangeSlider.LowerValue, Math.Max( rangeSlider.Minimum, Math.Min( rangeSlider.Maximum, newValue ) ) );
  158. }
  159. private static void OnHigherValueChanged( DependencyObject sender, DependencyPropertyChangedEventArgs args )
  160. {
  161. RangeSlider rangeSlider = sender as RangeSlider;
  162. if( rangeSlider != null )
  163. {
  164. rangeSlider.OnHigherValueChanged( ( double )args.OldValue, ( double )args.NewValue );
  165. }
  166. }
  167. protected virtual void OnHigherValueChanged( double oldValue, double newValue )
  168. {
  169. this.AdjustWidths( this.Minimum, this.Maximum, this.LowerValue, newValue );
  170. RoutedEventArgs args = new RoutedEventArgs();
  171. args.RoutedEvent = RangeSlider.HigherValueChangedEvent;
  172. this.RaiseEvent(args);
  173. }
  174. #endregion HigherValue
  175. #region LowerRangeBackground
  176. /// <summary>
  177. /// # TODODOC
  178. /// Get/Set the Brush for the Range between minimum/lower values . (Brush)
  179. /// </summary>
  180. public static readonly DependencyProperty LowerRangeBackgroundProperty = DependencyProperty.Register( "LowerRangeBackground", typeof( Brush ), typeof( RangeSlider ) );
  181. public Brush LowerRangeBackground
  182. {
  183. get
  184. {
  185. return ( Brush )GetValue( RangeSlider.LowerRangeBackgroundProperty );
  186. }
  187. set
  188. {
  189. SetValue( RangeSlider.LowerRangeBackgroundProperty, value );
  190. }
  191. }
  192. #endregion LowerRangeBackground
  193. #region LowerRangeStyle
  194. /// <summary>
  195. /// # TODODOC
  196. /// Get/Set the Style for the Range between Minimum/Lower values. (Style)
  197. /// </summary>
  198. public static readonly DependencyProperty LowerRangeStyleProperty = DependencyProperty.Register( "LowerRangeStyle", typeof( Style ), typeof( RangeSlider )
  199. , new FrameworkPropertyMetadata( null ) );
  200. public Style LowerRangeStyle
  201. {
  202. get
  203. {
  204. return ( Style )this.GetValue( RangeSlider.LowerRangeStyleProperty );
  205. }
  206. set
  207. {
  208. this.SetValue( RangeSlider.LowerRangeStyleProperty, value );
  209. }
  210. }
  211. #endregion LowerRangeStyle
  212. #region LowerRangeWidth
  213. /// <summary>
  214. /// # TODODOC
  215. ///
  216. /// LowerRangeWidth property is a readonly property, used to calculate the percentage of the LowerRange, within the entire min/max range.
  217. /// </summary>
  218. ///
  219. private static DependencyPropertyKey LowerRangeWidthPropertyKey = DependencyProperty.RegisterAttachedReadOnly( "LowerRangeWidth", typeof( double )
  220. , typeof( RangeSlider ), new PropertyMetadata( 0d ) );
  221. public static readonly DependencyProperty LowerRangeWidthProperty = LowerRangeWidthPropertyKey.DependencyProperty;
  222. public double LowerRangeWidth
  223. {
  224. get
  225. {
  226. return ( double )GetValue( RangeSlider.LowerRangeWidthProperty );
  227. }
  228. private set
  229. {
  230. SetValue( RangeSlider.LowerRangeWidthPropertyKey, value );
  231. }
  232. }
  233. #endregion LowerRangeWidth
  234. #region LowerThumbBackground
  235. /// <summary>
  236. /// # TODODOC
  237. /// Get/Set the Brush for the LowerValue thumb back of the icons [active state]. (Brush)
  238. /// </summary>
  239. public static readonly DependencyProperty LowerThumbBackgroundProperty = DependencyProperty.Register( "LowerThumbBackground", typeof( Brush ), typeof( RangeSlider ) );
  240. public Brush LowerThumbBackground
  241. {
  242. get
  243. {
  244. return ( Brush )GetValue( RangeSlider.LowerThumbBackgroundProperty );
  245. }
  246. set
  247. {
  248. SetValue( RangeSlider.LowerThumbBackgroundProperty, value );
  249. }
  250. }
  251. #endregion LowerThumbBackground
  252. #region LowerValue
  253. /// <summary>
  254. /// # TODODOC
  255. /// LowerValue property represents the lower value within the selected range.
  256. /// </summary>
  257. public static readonly DependencyProperty LowerValueProperty = DependencyProperty.Register( "LowerValue", typeof( double ), typeof( RangeSlider )
  258. , new FrameworkPropertyMetadata( RangeSlider.OnLowerValueChanged, RangeSlider.CoerceLowerValue ) );
  259. public double LowerValue
  260. {
  261. get
  262. {
  263. return ( double )GetValue( RangeSlider.LowerValueProperty );
  264. }
  265. set
  266. {
  267. SetValue( RangeSlider.LowerValueProperty, value );
  268. }
  269. }
  270. private static object CoerceLowerValue( DependencyObject sender, object value )
  271. {
  272. RangeSlider rangeSlider = sender as RangeSlider;
  273. double newValue = ( double )value;
  274. if( ( rangeSlider.HigherValue == 0 ) && ( rangeSlider.LowerValue == 0 ) )
  275. newValue = Math.Max( rangeSlider.Minimum, Math.Min( rangeSlider.Maximum, newValue ) );
  276. else
  277. newValue = Math.Min( Math.Max( rangeSlider.Minimum, Math.Min( rangeSlider.Maximum, newValue ) ), rangeSlider.HigherValue );
  278. return newValue;
  279. }
  280. private static void OnLowerValueChanged( DependencyObject sender, DependencyPropertyChangedEventArgs args )
  281. {
  282. RangeSlider rangeSlider = sender as RangeSlider;
  283. if( rangeSlider != null )
  284. {
  285. rangeSlider.OnLowerValueChanged( ( double )args.OldValue, ( double )args.NewValue );
  286. }
  287. }
  288. protected virtual void OnLowerValueChanged( double oldValue, double newValue )
  289. {
  290. this.HigherValue = Math.Max( this.HigherValue, newValue );
  291. this.AdjustWidths( this.Minimum, this.Maximum, newValue, this.HigherValue );
  292. RoutedEventArgs args = new RoutedEventArgs();
  293. args.RoutedEvent = RangeSlider.LowerValueChangedEvent;
  294. this.RaiseEvent(args);
  295. }
  296. #endregion LowerValue
  297. #region Maximum
  298. /// <summary>
  299. /// # TODODOC
  300. /// Maximum property represents the maximum value, which can be selected, in a range.
  301. /// </summary>
  302. public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register( "Maximum", typeof( double ), typeof( RangeSlider )
  303. , new FrameworkPropertyMetadata( RangeSlider.OnMaximumChanged, RangeSlider.CoerceMaximum ) );
  304. public double Maximum
  305. {
  306. get
  307. {
  308. return ( double )GetValue( RangeSlider.MaximumProperty );
  309. }
  310. set
  311. {
  312. SetValue( RangeSlider.MaximumProperty, value );
  313. }
  314. }
  315. private static object CoerceMaximum( DependencyObject sender, object value )
  316. {
  317. RangeSlider rangeSlider = sender as RangeSlider;
  318. double newValue = ( double )value;
  319. return Math.Max( newValue, rangeSlider.HigherValue );
  320. }
  321. private static void OnMaximumChanged( DependencyObject sender, DependencyPropertyChangedEventArgs args )
  322. {
  323. RangeSlider rangeSlider = sender as RangeSlider;
  324. if( rangeSlider != null )
  325. {
  326. rangeSlider.OnMaximumChanged( ( double )args.OldValue, ( double )args.NewValue );
  327. }
  328. }
  329. protected virtual void OnMaximumChanged( double oldValue, double newValue )
  330. {
  331. this.AdjustWidths( this.Minimum, newValue, this.LowerValue, this.HigherValue );
  332. }
  333. #endregion Maximum
  334. #region Minimum
  335. /// <summary>
  336. /// //#TODODOC
  337. /// Minimum property represents the minimum value, which can be selected, in a range.
  338. /// </summary>
  339. public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register( "Minimum", typeof( double ), typeof( RangeSlider )
  340. , new FrameworkPropertyMetadata( RangeSlider.OnMinimumChanged, RangeSlider.CoerceMinimum ) );
  341. public double Minimum
  342. {
  343. get
  344. {
  345. return ( double )GetValue( RangeSlider.MinimumProperty );
  346. }
  347. set
  348. {
  349. SetValue( RangeSlider.MinimumProperty, value );
  350. }
  351. }
  352. private static object CoerceMinimum( DependencyObject sender, object value )
  353. {
  354. RangeSlider rangeSlider = sender as RangeSlider;
  355. double newValue = ( double )value;
  356. if( ( rangeSlider.Minimum == 0.0 ) && ( rangeSlider.LowerValue == 0 ) && ( rangeSlider.HigherValue == 0 ) && ( rangeSlider.Maximum == 0 ) )
  357. {
  358. newValue = Math.Max( newValue, rangeSlider.LowerValue );
  359. }
  360. else
  361. {
  362. newValue = Math.Min( newValue, rangeSlider.LowerValue );
  363. }
  364. return newValue;
  365. }
  366. private static void OnMinimumChanged( DependencyObject sender, DependencyPropertyChangedEventArgs args )
  367. {
  368. RangeSlider rangeSlider = sender as RangeSlider;
  369. if( rangeSlider != null )
  370. {
  371. rangeSlider.OnMinimumChanged( ( double )args.OldValue, ( double )args.NewValue );
  372. }
  373. }
  374. protected virtual void OnMinimumChanged( double oldValue, double newValue )
  375. {
  376. // adjust the range width
  377. this.AdjustWidths( newValue, this.Maximum, this.LowerValue, this.HigherValue );
  378. }
  379. #endregion Minimum
  380. #region Orientation
  381. /// <summary>
  382. /// # TODODOC
  383. /// Get/Set the RangeSlider orientation.
  384. /// </summary>
  385. public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register( "Orientation", typeof( OrientationEnum ), typeof( RangeSlider ),
  386. new FrameworkPropertyMetadata( OrientationEnum.Horizontal, RangeSlider.OnOrientationChanged ) );
  387. public OrientationEnum Orientation
  388. {
  389. get
  390. {
  391. return ( OrientationEnum )GetValue( RangeSlider.OrientationProperty );
  392. }
  393. set
  394. {
  395. SetValue( RangeSlider.OrientationProperty, value );
  396. }
  397. }
  398. private static void OnOrientationChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
  399. {
  400. RangeSlider rangeSlider = sender as RangeSlider;
  401. if( rangeSlider != null )
  402. {
  403. rangeSlider.OnOrientationChanged( ( OrientationEnum )e.OldValue, ( OrientationEnum )e.NewValue );
  404. }
  405. }
  406. protected virtual void OnOrientationChanged( OrientationEnum oldValue, OrientationEnum newValue )
  407. {
  408. }
  409. #endregion //Orientation
  410. #region RangeBackground
  411. /// <summary>
  412. /// # TODODOC
  413. /// Get/Set the Brush for the Range between lower/higher values [active state]. (Brush)
  414. /// </summary>
  415. public static readonly DependencyProperty RangeBackgroundProperty = DependencyProperty.Register( "RangeBackground", typeof( Brush ), typeof( RangeSlider ) );
  416. public Brush RangeBackground
  417. {
  418. get
  419. {
  420. return ( Brush )GetValue( RangeSlider.RangeBackgroundProperty );
  421. }
  422. set
  423. {
  424. SetValue( RangeSlider.RangeBackgroundProperty, value );
  425. }
  426. }
  427. #endregion RangeBackground
  428. #region RangeStyle
  429. /// <summary>
  430. /// # TODODOC
  431. /// Get/Set the Style for the Range between Lower/Higher values. (Style)
  432. /// </summary>
  433. public static readonly DependencyProperty RangeStyleProperty = DependencyProperty.Register( "RangeStyle", typeof( Style ), typeof( RangeSlider )
  434. , new FrameworkPropertyMetadata( null ) );
  435. public Style RangeStyle
  436. {
  437. get
  438. {
  439. return ( Style )this.GetValue( RangeSlider.RangeStyleProperty );
  440. }
  441. set
  442. {
  443. this.SetValue( RangeSlider.RangeStyleProperty, value );
  444. }
  445. }
  446. #endregion RangeStyle
  447. #region RangeWidth
  448. /// <summary>
  449. /// # TODODOC
  450. ///
  451. /// RangeWidth property is a readonly property, used to calculate the percentage of the range within the entire min/max range.
  452. /// </summary>
  453. private static readonly DependencyPropertyKey RangeWidthPropertyKey = DependencyProperty.RegisterAttachedReadOnly( "RangeWidth", typeof( double )
  454. , typeof( RangeSlider ), new PropertyMetadata( 0d ) );
  455. public static readonly DependencyProperty RangeWidthProperty = RangeWidthPropertyKey.DependencyProperty;
  456. public double RangeWidth
  457. {
  458. get
  459. {
  460. return ( double )GetValue( RangeSlider.RangeWidthProperty );
  461. }
  462. private set
  463. {
  464. SetValue( RangeSlider.RangeWidthPropertyKey, value );
  465. }
  466. }
  467. #endregion RangeWidth
  468. #region Step
  469. /// <summary>
  470. /// # TODODOC
  471. ///
  472. /// Step property is used to identify the RangeSlider's size of individual move, while clicking on the LowerRange, HigherRange, not while scrolling the thumbs.
  473. /// </summary>
  474. private static readonly DependencyProperty StepProperty = DependencyProperty.Register( "Step", typeof( double ), typeof( RangeSlider )
  475. , new PropertyMetadata( 1.0, null, RangeSlider.CoerceStep ) );
  476. public double Step
  477. {
  478. get
  479. {
  480. return ( double )GetValue( RangeSlider.StepProperty );
  481. }
  482. set
  483. {
  484. SetValue( RangeSlider.StepProperty, value );
  485. }
  486. }
  487. private static object CoerceStep( DependencyObject sender, object value )
  488. {
  489. RangeSlider rangeSlider = sender as RangeSlider;
  490. double newValue = ( double )value;
  491. return Math.Max( 0.01, newValue );
  492. }
  493. #endregion
  494. #endregion Properties
  495. #region Override
  496. public override void OnApplyTemplate()
  497. {
  498. base.OnApplyTemplate();
  499. if (_lowerRange != null)
  500. {
  501. _lowerRange.Click -= new RoutedEventHandler(this.LowerRange_Click);
  502. }
  503. _lowerRange = this.Template.FindName(PART_LowerRange, this) as RepeatButton;
  504. if (_lowerRange != null)
  505. {
  506. _lowerRange.Click += new RoutedEventHandler(this.LowerRange_Click);
  507. }
  508. if (_higherRange != null)
  509. {
  510. _higherRange.Click -= new RoutedEventHandler(this.HigherRange_Click);
  511. }
  512. _higherRange = this.Template.FindName(PART_HigherRange, this) as RepeatButton;
  513. if (_higherRange != null)
  514. {
  515. _higherRange.Click += new RoutedEventHandler(this.HigherRange_Click);
  516. }
  517. if( _lowerSlider != null )
  518. {
  519. _lowerSlider.Loaded -= this.Slider_Loaded;
  520. }
  521. _lowerSlider = this.Template.FindName( PART_LowerSlider, this ) as Slider;
  522. if( _lowerSlider != null )
  523. {
  524. _lowerSlider.Loaded += this.Slider_Loaded;
  525. }
  526. if( _higherSlider != null )
  527. {
  528. _higherSlider.Loaded -= this.Slider_Loaded;
  529. }
  530. _higherSlider = this.Template.FindName( PART_HigherSlider, this ) as Slider;
  531. if( _higherSlider != null )
  532. {
  533. _higherSlider.Loaded += this.Slider_Loaded;
  534. }
  535. }
  536. public override string ToString()
  537. {
  538. return this.LowerValue.ToString() + "-" + this.HigherValue.ToString();
  539. }
  540. #endregion Override
  541. #region Methods
  542. internal static double GetThumbWidth( Slider slider )
  543. {
  544. if( slider != null )
  545. {
  546. var track = ( Track )slider.Template.FindName( "PART_Track", slider );
  547. if( track != null )
  548. {
  549. var thumb = track.Thumb;
  550. return thumb.ActualWidth;
  551. }
  552. }
  553. return 0d;
  554. }
  555. internal static double GetThumbHeight( Slider slider )
  556. {
  557. if( slider != null )
  558. {
  559. var track = ( Track )slider.Template.FindName( "PART_Track", slider );
  560. if( track != null )
  561. {
  562. var thumb = track.Thumb;
  563. return thumb.ActualHeight;
  564. }
  565. }
  566. return 0d;
  567. }
  568. private void AdjustWidths( double minimum, double maximum, double lowerValue, double higherValue )
  569. {
  570. double actualWidth = 0;
  571. double lowerSliderThumbWidth = 0d;
  572. double higherSliderThumbWidth = 0d;
  573. if( this.Orientation == OrientationEnum.Horizontal )
  574. {
  575. actualWidth = this.ActualWidth;
  576. lowerSliderThumbWidth = RangeSlider.GetThumbWidth( _lowerSlider );
  577. higherSliderThumbWidth = RangeSlider.GetThumbWidth( _higherSlider );
  578. }
  579. else if( this.Orientation == OrientationEnum.Vertical )
  580. {
  581. actualWidth = this.ActualHeight;
  582. lowerSliderThumbWidth = RangeSlider.GetThumbHeight( _lowerSlider );
  583. higherSliderThumbWidth = RangeSlider.GetThumbHeight( _higherSlider );
  584. }
  585. actualWidth -= ( lowerSliderThumbWidth + higherSliderThumbWidth );
  586. double entireRange = maximum - minimum;
  587. this.HigherRangeWidth = ( actualWidth * ( maximum - higherValue ) ) / entireRange;
  588. this.RangeWidth = ( actualWidth * ( higherValue - lowerValue ) ) / entireRange;
  589. this.LowerRangeWidth = ( actualWidth * ( lowerValue - minimum ) ) / entireRange;
  590. }
  591. private void SetSlidersMargins()
  592. {
  593. if( ( _lowerSlider != null ) && ( _higherSlider != null ) )
  594. {
  595. if( this.Orientation == OrientationEnum.Horizontal )
  596. {
  597. double lowerSliderThumbWidth = RangeSlider.GetThumbWidth( _lowerSlider );
  598. double higherSliderThumbWidth = RangeSlider.GetThumbWidth( _higherSlider );
  599. _higherSlider.Margin = new Thickness( lowerSliderThumbWidth, 0d, 0d, 0d );
  600. _lowerSlider.Margin = new Thickness( 0d, 0d, higherSliderThumbWidth, 0d );
  601. }
  602. else
  603. {
  604. double lowerSliderThumbHeight = RangeSlider.GetThumbHeight( _lowerSlider );
  605. double higherSliderThumbHeight = RangeSlider.GetThumbHeight( _higherSlider );
  606. _higherSlider.Margin = new Thickness( 0d, 0d, 0d, lowerSliderThumbHeight );
  607. _lowerSlider.Margin = new Thickness( 0d, higherSliderThumbHeight, 0d, 0d );
  608. }
  609. }
  610. }
  611. #endregion
  612. #region Events
  613. public static readonly RoutedEvent LowerValueChangedEvent = EventManager.RegisterRoutedEvent("LowerValueChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RangeSlider));
  614. public event RoutedEventHandler LowerValueChanged
  615. {
  616. add
  617. {
  618. AddHandler( RangeSlider.LowerValueChangedEvent, value );
  619. }
  620. remove
  621. {
  622. RemoveHandler( RangeSlider.LowerValueChangedEvent, value );
  623. }
  624. }
  625. public static readonly RoutedEvent HigherValueChangedEvent = EventManager.RegisterRoutedEvent("HigherValueChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RangeSlider));
  626. public event RoutedEventHandler HigherValueChanged
  627. {
  628. add
  629. {
  630. AddHandler( RangeSlider.HigherValueChangedEvent, value );
  631. }
  632. remove
  633. {
  634. RemoveHandler( RangeSlider.HigherValueChangedEvent, value );
  635. }
  636. }
  637. #endregion //Events
  638. #region Events Handlers
  639. private void LowerRange_Click(object sender, RoutedEventArgs e)
  640. {
  641. this.LowerValue -= this.Step;
  642. }
  643. private void HigherRange_Click(object sender, RoutedEventArgs e)
  644. {
  645. this.HigherValue += this.Step;
  646. }
  647. private void RangeSlider_SizeChanged( object sender, SizeChangedEventArgs e )
  648. {
  649. this.AdjustWidths( this.Minimum, this.Maximum, this.LowerValue, this.HigherValue );
  650. }
  651. private void Slider_Loaded( object sender, RoutedEventArgs e )
  652. {
  653. this.SetSlidersMargins();
  654. this.AdjustWidths( this.Minimum, this.Maximum, this.LowerValue, this.HigherValue );
  655. }
  656. #endregion Events Handlers
  657. }
  658. }