@ -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 ( _f ireSelectionChangedEvent )
SelectDateTimePart ( ) ;
PerformMouseSelection ( ) ;
else
_f ireSelectionChangedEvent = true ;
}
@ -185,6 +195,8 @@ namespace Microsoft.Windows.Controls
#endregion //Abstract
#region Private
private void InitializeDateTimeInfoList ( )
{
_d ateTimeInfoList . Clear ( ) ;
@ -372,21 +384,54 @@ namespace Microsoft.Windows.Controls
} ) ;
}
private void SelectDateTimePart ( )
private void PerformMouseSelection ( )
{
_d ateTimeInfoList . ForEach ( info = >
{
if ( ( info . StartPosition < = TextBox . SelectionStart ) & & ( TextBox . SelectionStart < ( info . StartPosition + info . Length ) ) )
{
_f ireSelectionChangedEvent = false ;
TextBox . Select ( info . StartPosition , info . Length ) ;
_f ireSelectionChangedEvent = true ;
_ selectedDateTimeInfo = info ;
Select ( info ) ;
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 = _d ateTimeInfoList . IndexOf ( _ selectedDateTimeInfo ) ;
//make sure we stay within the selection ranges
if ( ( index = = 0 & & direction = = - 1 ) | | ( index = = _d ateTimeInfoList . Count - 1 & & direction = = 1 ) )
return ;
//get the DateTimeInfo at the next position
index + = direction ;
info = _d ateTimeInfoList [ index ] ;
//we don't care about spaces and commas, only select valid DateTimeInfos
while ( info . Type = = DateTimePart . Other )
{
info = _d ateTimeInfoList [ index + = direction ] ;
}
//perform selection
Select ( info ) ;
}
private void Select ( DateTimeInfo info )
{
_f ireSelectionChangedEvent = false ;
TextBox . Select ( info . StartPosition , info . Length ) ;
_f ireSelectionChangedEvent = true ;
_ selectedDateTimeInfo = info ;
}
private string GetFormatString ( DateTimeFormat dateTimeFormat )
{
switch ( dateTimeFormat )
@ -484,6 +529,8 @@ namespace Microsoft.Windows.Controls
_f ireSelectionChangedEvent = true ;
}
#endregion //Private
#endregion //Methods
}
}