Hi,
I have extended the DateTimeEditBox class with functionality for auto-selecting all the text when the user clicks a "Part" or uses the left and right arrow keys:
public class MyDateTimeEditBox : DateTimeEditBox
{
public MyDateTimeEditBox()
{
PreviewMouseDown += new MouseButtonEventHandler(MyDateTimeEditBox_PreviewMouseDown);
PreviewKeyDown += new KeyEventHandler(MyDateTimeEditBox_PreviewKeyDown);
}
void MyDateTimeEditBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Part part = e.Source as Part;
if (part != null)
{
MaskedTextBox mtb = (MaskedTextBox)VisualTreeHelperExtended.GetFirstDescendant(part, typeof(MaskedTextBox));
if (mtb != null)
{
mtb.Focus();
mtb.SelectAll();
e.Handled = true;
}
}
}
void MyDateTimeEditBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
MaskedTextBox mtb = Keyboard.FocusedElement as MaskedTextBox;
if (mtb != null)
{
if (e.Key == Key.Right)
{
mtb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
mtb = Keyboard.FocusedElement as MaskedTextBox;
mtb.SelectAll();
e.Handled = true;
}
else if (e.Key == Key.Left)
{
mtb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
mtb = Keyboard.FocusedElement as MaskedTextBox;
mtb.SelectAll();
e.Handled = true;
}
}
}
}
This works fine except when I'm using the "MMM" custom format specifier. When I set Format="dd MMM yyyy HH:mm:ss", I'm not able to click the month-part, and when I use the left and right arrow-keys, the month-part is skipped. When tracing the code, I can see that it enters the "MyDateTimeEditBox_PreviewMouseDown" with the correct MaskedTextBox, but the month-part will not become selected.
If I remove the e.Handled=true in "MyDateTimeEditBox_PreviewMouseDown", I can click the month part, but the text is not selected.
Is this a "missing feature" or do I need to implement it in another way?
Thanks,
Geir