Reputation: 4581
I have a delegate command which when invoked I want to lose focus on any control which has focus. I am using MVVM therefore the code in the delegate command's execute has no reference to the UIElements. I am happy to move focus or use any kind of trick to this - but it needs to be somewhat agnostic to which control has focus.
Don't mind if the solution is in XAML or C#.
Upvotes: 0
Views: 28
Reputation: 69979
Perhaps you should be using the TraversalRequest
class to move the focus to the next control? From the linked page, it:
Represents a request to move focus to another control.
All you need to get this to work is the currently focused element. Again, from the linked page on MSDN:
// Creating a FocusNavigationDirection object and setting it to a
// local field that contains the direction selected.
FocusNavigationDirection focusDirection = _focusMoveValue;
// MoveFocus takes a TraveralReqest as its argument.
TraversalRequest request = new TraversalRequest(focusDirection);
// Gets the element with keyboard focus.
UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
// Change keyboard focus.
if (elementWithFocus != null)
{
elementWithFocus.MoveFocus(request);
}
Upvotes: 1