WPF:如何以编程方式从文本框中删除焦点 [英] WPF: How to programmatically remove focus from a TextBox

查看:871
本文介绍了WPF:如何以编程方式从文本框中删除焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的WPF TextBox 添加一个简单的(至少我认为是)行为。



当用户按Escape我想要 TextBox 他正在编辑有用户开始编辑时的文本,并且我想从 TextBox



我没有任何问题,在编辑开始时为它的值设置文本。 / p>

问题是删除元素的焦点。我不想将焦点移动到任何其他组件,我只是希望 TextBox 失去焦点。我必须有一个不可见的元素来设置焦点,这样我的 TextBox 可以失去焦点吗?

解决方案

在.NET Framework 4中 Keyboard.ClearFocus();


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?

解决方案

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

这篇关于WPF:如何以编程方式从文本框中删除焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆