I've implemented an attached property that when applied, will watch for the PreviewKeyDown event and call:
item.MoveFocus(
new TraversalRequest(FocusNavigationDirection.Next));
when it sees the Enter key pressed. This works great for standard WPF controls (i.e. TextBox, ComboBox, etc), however the Infragistics masked edit controls (i.e. XamDateTimeEditor, and XamCurrencyEditor) don't work as expected. When I hit the TAB key to put focus onto a masked edit control, it works correctly. However, when I call the above line of code, the SelectionsList element of the masked editor gets focus. When this happens, I'm unable to edit or call MoveFocus to get out of the control.
Any ideas? I've attached a file with the code I'm using to call MoveFocus.
Two things stand out to me. First, you're using the e.Source which is going to be the sender (the xamMaskedEditor in this case) and not the element with focus for which the event is being raised. You should probably be using e.OriginalSource. Second, you should probably be marking the event as handled or the control that is receiving the input focus is going to receive an Enter key press.
Your first point makes sense. The second point was the one that fixed my issue. I left the event unhandled on purpose because I wanted to be as unintrusive as possible. I'm still not clear on why marking it as handled has corrected my issue, other than maybe the infragistics controls are reacting to that key press and are doing something to undo my navigation. I can see how marking handled would make sense so I'm fine with that change. Thanks!
To follow up on this, one of the things I started seeing was the user using the keyboard to select an item in a standard ComboBox. Since I'm intercepting the KeyDown event and setting e.Handled = true, the ComboBox was never getting a chance to handle the KeyPress event, and thus it wasn't selecting the item in the drop down. I ended up doing the following, which seems to work with all controls:
control.AddHandler(UIElement.KeyDownEvent, (KeyEventHandler)OnKeyDown, true);
...
OnKeyDown
((UIElement)sender).MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
e.Handled = true;
The trick for me was to use the KeyDown event instead of the PreviewKeyDown event and to use sender since the combo list item would be the e.OriginalSource and thus would select the next item in the ComboBox list. This actually makes sense to me since I want to move focus away from the control that I've added a handler for, not necessarily some control inside the control who's event I'm handling.
The other thing was to use the AddHandler method so I could tell WPF to notify me even if the control has handled the event.
Anyway, this approach seems to work fine for both the standard WPF controls and the Infragistics controls.