WPF:如何以编程方式从 TextBox 中移除焦点 [英] WPF: How to programmatically remove focus from a TextBox
问题描述
我想在我的 WPF TextBox
中添加一个简单的(至少我认为是)行为.
I want to add a simple (at least I thought it was) behaviour to my WPF TextBox
.
当用户按下 Escape 时,我希望他正在编辑的 TextBox
拥有用户开始编辑时所拥有的文本,并且我想从 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.
问题是移除元素的焦点.我不想将焦点移到任何其他组件,我只想让 TextBox
失去焦点.我是否必须有一个不可见的元素来设置焦点,这样我的 TextBox
才能失去焦点?
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?
推荐答案
在 .NET Framework 4 中只需 Keyboard.ClearFocus();
in .NET Framework 4 just Keyboard.ClearFocus();
这篇关于WPF:如何以编程方式从 TextBox 中移除焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!