WPF: How to programmatically remove focus from a TextBox
The code I have been using :
// Move to a parent that can take focusFrameworkElement parent = (FrameworkElement)textBox.Parent;while (parent != null && parent is IInputElement && !((IInputElement)parent).Focusable){ parent = (FrameworkElement)parent.Parent;}DependencyObject scope = FocusManager.GetFocusScope(textBox);FocusManager.SetFocusedElement(scope, parent as IInputElement);
Since none of the above answers worked for me and the accepted answer does work only for a keyboard focus, I came to the following approach:
// Kill logical focusFocusManager.SetFocusedElement(FocusManager.GetFocusScope(textBox), null);// Kill keyboard focusKeyboard.ClearFocus();
Kills both, logical as well as the keyboard focus.