PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/SharpFellows.Toolkit/Controls/DomainUpDown.cs

https://bitbucket.org/suhas_28/sharpfellows.toolkit
C# | 185 lines | 126 code | 31 blank | 28 comment | 30 complexity | 970a0cbb3e2ec4d65c41943bd5f4dbe3 MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Controls.Primitives;
  7. using System.Windows.Input;
  8. namespace SharpFellows.Toolkit.Controls
  9. {
  10. /// <summary>
  11. /// WPF DomainUpDown control
  12. /// </summary>
  13. [TemplatePart(Name = "PART_UpButton", Type = typeof(RepeatButton))]
  14. [TemplatePart(Name = "PART_DownButton", Type = typeof(RepeatButton))]
  15. [TemplatePart(Name = "PART_TextBox", Type = typeof(TextBox))]
  16. public class DomainUpDown : Control
  17. {
  18. #region Fields
  19. private int _selectedIndex;
  20. private RepeatButton _upBuppton;
  21. private RepeatButton _downButton;
  22. #endregion
  23. #region Dependency properties
  24. public static readonly DependencyProperty ValueProperty =
  25. DependencyProperty.Register("Value", typeof (object), typeof (DomainUpDown), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged));
  26. public static readonly DependencyProperty ItemsProperty =
  27. DependencyProperty.Register("Items", typeof(IEnumerable), typeof(DomainUpDown), new PropertyMetadata(OnItemsChanged));
  28. #endregion
  29. /// <summary>
  30. /// Initializes the <see cref="DomainUpDown"/> class.
  31. /// </summary>
  32. static DomainUpDown()
  33. {
  34. DefaultStyleKeyProperty.OverrideMetadata(typeof(DomainUpDown), new FrameworkPropertyMetadata(typeof(DomainUpDown)));
  35. BorderBrushProperty.OverrideMetadata(typeof(DomainUpDown), new FrameworkPropertyMetadata(SystemColors.ControlLightBrush));
  36. }
  37. /// <summary>
  38. /// Gets or sets the selected value.
  39. /// </summary>
  40. /// <value>The value.</value>
  41. public object Value
  42. {
  43. get { return GetValue(ValueProperty); }
  44. set { SetValue(ValueProperty, value); }
  45. }
  46. /// <summary>
  47. /// Gets or sets the items.
  48. /// </summary>
  49. /// <value>The items.</value>
  50. public IEnumerable Items
  51. {
  52. get { return GetValue(ItemsProperty) as IEnumerable; }
  53. set { SetValue(ItemsProperty, value);}
  54. }
  55. /// <summary>
  56. /// Gets or sets the index of the selected value.
  57. /// </summary>
  58. /// <value>The index of the selected.</value>
  59. protected int SelectedIndex
  60. {
  61. get { return _selectedIndex; }
  62. set
  63. {
  64. if (_selectedIndex == value)
  65. return;
  66. _selectedIndex = value;
  67. Value = Items.Cast<object>().Skip(SelectedIndex).First();
  68. }
  69. }
  70. protected override void OnPreviewKeyDown(KeyEventArgs e)
  71. {
  72. base.OnPreviewKeyDown(e);
  73. if (e.Key == Key.Down)
  74. {
  75. if (_downButton != null)
  76. _downButton.Focus();
  77. OnDown(this, null);
  78. e.Handled = true;
  79. }
  80. if (e.Key == Key.Up)
  81. {
  82. if (_upBuppton != null)
  83. _upBuppton.Focus();
  84. OnUp(this, null);
  85. e.Handled = true;
  86. }
  87. }
  88. /// <summary>
  89. /// When overridden in a derived class, is invoked whenever application code or internal processes call
  90. /// <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
  91. /// </summary>
  92. public override void OnApplyTemplate()
  93. {
  94. base.OnApplyTemplate();
  95. if (_upBuppton != null)
  96. _upBuppton.Click -= OnUp;
  97. if (_downButton != null)
  98. _downButton.Click -= OnDown;
  99. // Get the parts and attach event handlers to them
  100. _upBuppton = GetTemplateChild("PART_UpButton") as RepeatButton;
  101. _downButton = GetTemplateChild("PART_DownButton") as RepeatButton;
  102. if (_upBuppton != null)
  103. _upBuppton.Click += OnUp;
  104. if (_downButton != null)
  105. _downButton.Click += OnDown;
  106. }
  107. /// <summary>
  108. /// Invoked whenever an unhandled <see cref="E:System.Windows.UIElement.GotFocus"/> event reaches this element in its route.
  109. /// </summary>
  110. /// <param name="e">The <see cref="T:System.Windows.RoutedEventArgs"/> that contains the event data.</param>
  111. protected override void OnGotFocus(RoutedEventArgs e)
  112. {
  113. base.OnGotFocus(e);
  114. // Move focus immediately to the buttons
  115. if (_upBuppton != null)
  116. _upBuppton.Focus();
  117. }
  118. private void OnUp(object sender, RoutedEventArgs routedEventArgs)
  119. {
  120. if (SelectedIndex > 0)
  121. SelectedIndex--;
  122. }
  123. private void OnDown(object sender, RoutedEventArgs routedEventArgs)
  124. {
  125. if (Items != null && SelectedIndex < Items.Cast<object>().Count() - 1)
  126. SelectedIndex++;
  127. }
  128. private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  129. {
  130. var updown = d as DomainUpDown;
  131. SynchroniseValueWithIndex(updown, e.NewValue);
  132. }
  133. private static void OnItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  134. {
  135. var updown = d as DomainUpDown;
  136. SynchroniseValueWithIndex(updown, updown == null? null : updown.Value);
  137. }
  138. private static void SynchroniseValueWithIndex(DomainUpDown updown, object newValue)
  139. {
  140. if (updown == null || updown.Items == null)
  141. return;
  142. int i = 0;
  143. foreach (var element in updown.Items)
  144. {
  145. if (element.Equals(newValue))
  146. {
  147. updown.SelectedIndex = i;
  148. break;
  149. }
  150. i++;
  151. }
  152. }
  153. }
  154. }