Browse Source

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.

import/raw
brianlagunas_cp 16 years ago
parent
commit
1f522971e0
  1. 59
      Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs

59
Main/Source/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs

@ -110,6 +110,16 @@ namespace Microsoft.Windows.Controls
Value = null; Value = null;
break; break;
} }
case Key.Left:
{
PerformKeyboardSelection(-1);
break;
}
case Key.Right:
{
PerformKeyboardSelection(1);
break;
}
default: default:
{ {
break; break;
@ -147,7 +157,7 @@ namespace Microsoft.Windows.Controls
void TextBox_SelectionChanged(object sender, RoutedEventArgs e) void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{ {
if (_fireSelectionChangedEvent) if (_fireSelectionChangedEvent)
SelectDateTimePart(); PerformMouseSelection();
else else
_fireSelectionChangedEvent = true; _fireSelectionChangedEvent = true;
} }
@ -185,6 +195,8 @@ namespace Microsoft.Windows.Controls
#endregion //Abstract #endregion //Abstract
#region Private
private void InitializeDateTimeInfoList() private void InitializeDateTimeInfoList()
{ {
_dateTimeInfoList.Clear(); _dateTimeInfoList.Clear();
@ -372,21 +384,54 @@ namespace Microsoft.Windows.Controls
}); });
} }
private void SelectDateTimePart() private void PerformMouseSelection()
{ {
_dateTimeInfoList.ForEach(info => _dateTimeInfoList.ForEach(info =>
{ {
if ((info.StartPosition <= TextBox.SelectionStart) && (TextBox.SelectionStart < (info.StartPosition + info.Length))) if ((info.StartPosition <= TextBox.SelectionStart) && (TextBox.SelectionStart < (info.StartPosition + info.Length)))
{ {
_fireSelectionChangedEvent = false; Select(info);
TextBox.Select(info.StartPosition, info.Length);
_fireSelectionChangedEvent = true;
_selectedDateTimeInfo = info;
return; return;
} }
}); });
} }
/// <summary>
/// Performs the keyboard selection.
/// </summary>
/// <param name="direction">The direction.</param>
/// <remarks>-1 = Left, 1 = Right</remarks>
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) private string GetFormatString(DateTimeFormat dateTimeFormat)
{ {
switch (dateTimeFormat) switch (dateTimeFormat)
@ -484,6 +529,8 @@ namespace Microsoft.Windows.Controls
_fireSelectionChangedEvent = true; _fireSelectionChangedEvent = true;
} }
#endregion //Private
#endregion //Methods #endregion //Methods
} }
} }

Loading…
Cancel
Save