Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: How to programmatically remove focus from a TextBox

Tags:

focus

wpf

textbox

I want to add a simple (at least I thought it was) behaviour to my WPF TextBox.

When the user presses Escape I want the TextBox he is editing to have the text it had when the user started editing, AND I want to remove the focus from the TextBox.

I don't have any problem setting the text for the value it had in the beginning of the edit.

The problem is to remove the focus of the element. I don't want to move the focus to any other component, I just want the TextBox to lose focus. Will I have to have an invisible element to set the focus so my TextBox can lose focus?

like image 951
jpsstavares Avatar asked May 26 '10 15:05

jpsstavares


1 Answers

in .NET Framework 4 just Keyboard.ClearFocus();

like image 131
LPL Avatar answered Oct 05 '22 13:10

LPL