From 679bd24e0e3b5aed3655c908e0cd537d573260e0 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Tue, 15 Feb 2011 17:00:16 +0000 Subject: [PATCH] DateTimeUpDown: added Left and Right keyboard navigation so the user can use the keyboard to select the different DateTime parts instead of having to use the mouse everytime. --- .../DateTimeUpDown/DateTimeUpDown.cs | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs index ff97630c..5805b3dd 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs @@ -110,6 +110,16 @@ namespace Microsoft.Windows.Controls Value = null; break; } + case Key.Left: + { + PerformKeyboardSelection(-1); + break; + } + case Key.Right: + { + PerformKeyboardSelection(1); + break; + } default: { break; @@ -147,7 +157,7 @@ namespace Microsoft.Windows.Controls void TextBox_SelectionChanged(object sender, RoutedEventArgs e) { if (_fireSelectionChangedEvent) - SelectDateTimePart(); + PerformMouseSelection(); else _fireSelectionChangedEvent = true; } @@ -185,6 +195,8 @@ namespace Microsoft.Windows.Controls #endregion //Abstract + #region Private + private void InitializeDateTimeInfoList() { _dateTimeInfoList.Clear(); @@ -372,21 +384,54 @@ namespace Microsoft.Windows.Controls }); } - private void SelectDateTimePart() + private void PerformMouseSelection() { _dateTimeInfoList.ForEach(info => { if ((info.StartPosition <= TextBox.SelectionStart) && (TextBox.SelectionStart < (info.StartPosition + info.Length))) { - _fireSelectionChangedEvent = false; - TextBox.Select(info.StartPosition, info.Length); - _fireSelectionChangedEvent = true; - _selectedDateTimeInfo = info; + Select(info); return; } }); } + /// + /// Performs the keyboard selection. + /// + /// The direction. + /// -1 = Left, 1 = Right + private void PerformKeyboardSelection(int direction) + { + DateTimeInfo info; + int index = _dateTimeInfoList.IndexOf(_selectedDateTimeInfo); + + //make sure we stay within the selection ranges + if ((index == 0 && direction == -1) || (index == _dateTimeInfoList.Count - 1 && direction == 1)) + return; + + //get the DateTimeInfo at the next position + index += direction; + info = _dateTimeInfoList[index]; + + //we don't care about spaces and commas, only select valid DateTimeInfos + while (info.Type == DateTimePart.Other) + { + info = _dateTimeInfoList[index += direction]; + } + + //perform selection + Select(info); + } + + private void Select(DateTimeInfo info) + { + _fireSelectionChangedEvent = false; + TextBox.Select(info.StartPosition, info.Length); + _fireSelectionChangedEvent = true; + _selectedDateTimeInfo = info; + } + private string GetFormatString(DateTimeFormat dateTimeFormat) { switch (dateTimeFormat) @@ -484,6 +529,8 @@ namespace Microsoft.Windows.Controls _fireSelectionChangedEvent = true; } + #endregion //Private + #endregion //Methods } }