wpf GotFocus 和 GotKeyboardFocus 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18152056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between GotFocus and GotKeyboardFocus
提问by agent47
What is the difference(s) between GotFocusand GotKeyboardFocus-and similarly LostFocusand LostKeyboardFocus?
GotFocus和GotKeyboardFocus- 类似地LostFocus和之间有什么区别LostKeyboardFocus?
Sorry for the simple question, but, I googled it and read a lot of blog posts, but I'm still confused. It seems nobody knows exactly what is the difference ):
对不起,这个简单的问题,但是,我用谷歌搜索并阅读了很多博客文章,但我仍然感到困惑。似乎没有人知道到底有什么区别):
UPDATE:
更新:
My usage:
我的用法:
I am creating a custom control by extending Controlclass. Something like ComboBoxbut with some other effects. I'm trying to open and close a Popupby setting a property: IsDropDownOpenjust like a ComboBoxthrough the GotFocusand LostFocusevents. I don't want to Popupget closed, when I Alt+Tabed the windows, but get closed when I click on a Buttonfor example or I go to a TextBox. I did:
我正在通过扩展Control类创建自定义控件。有点像,ComboBox但有一些其他效果。我试图Popup通过设置一个属性来打开和关闭 a :IsDropDownOpen就像ComboBox通过GotFocus和LostFocus事件一样。Popup当我Alt+Tab打开窗户时,我不想关闭,但是当我单击 aButton或转到 a时关闭TextBox。我做了:
private static void OnGotFocusHandler(object sender, RoutedEventArgs e) {
if (e.Handled)
return;
((SearchBox)sender).IsDropDownOpen = true;
e.Handled = true;
}
private static void OnLostFocusHandler(object sender, RoutedEventArgs e) {
if (e.Handled)
return;
((SearchBox)sender).IsDropDownOpen = false;
e.Handled = true;
}
The GotFocusworks. But the Lostone didn't. If I do the Loststuff in LostKeyboardFocusthen when I Alt+Tabthe windows, or Windowgoes to inactive, then the method get called, while I don't want. How can I solve it?
该GotFocus作品。但那个Lost没有。如果我做的Lost东西在LostKeyboardFocus那么当我Alt+Tab的窗户,或Window进入未激活,则该方法被调用,而我不想。我该如何解决?
回答by Abe Heidebrecht
MSDNhas an overview of focus, but I'll try to explain it here.
MSDN有一个重点概述,但我将尝试在这里解释它。
WPF has 2 concepts regarding focus. There is the physical keyboard focus, and there is logical focus. Only one element can have keyboard focus (and if the application isn't the active application, no element will have keyboard focus).
WPF 有 2 个关于焦点的概念。有物理键盘焦点,也有逻辑焦点。只有一个元素可以拥有键盘焦点(如果应用程序不是活动应用程序,则没有元素将拥有键盘焦点)。
Multiple items can have logical focus. In fact, you can create new "focus scopes". As per MSDN:
多个项目可以有逻辑焦点。事实上,您可以创建新的“焦点范围”。根据 MSDN:
When keyboard focus leaves a focus scope, the focused element will lose keyboard focus but will retain logical focus. When keyboard focus returns to the focus scope, the focused element will obtain keyboard focus. This allows for keyboard focus to be changed between multiple focus scopes but ensures that the focused element in the focus scope regains keyboard focus when focus returns to the focus scope.
当键盘焦点离开焦点范围时,焦点元素将失去键盘焦点但会保留逻辑焦点。当键盘焦点返回焦点范围时,焦点元素将获得键盘焦点。这允许在多个焦点范围之间更改键盘焦点,但确保焦点范围中的焦点元素在焦点返回到焦点范围时重新获得键盘焦点。
You can define your own focus scope on an element (typically a Panel) by setting FocusManager.IsFocusScope="True". The controls in WPF that are focus scopes by default are Window, MenuItem, ToolBar, and ContextMenu.
您可以Panel通过设置FocusManager.IsFocusScope="True". 这是重点范围默认情况下,WPF控件是Window,MenuItem,ToolBar,和ContextMenu。
This makes sense if you think about having multiple Windows in your application. When you Alt-Tabbetween them, you expect your keyboard focus to return to the same place it was the last time the Windowhad focus. By keeping keyboard focus and logical focus separate, you can achieve this.
如果您考虑Window在您的应用程序中有多个s,这是有道理的。当您Alt-Tab在它们之间时,您希望键盘焦点返回到上次Window获得焦点时的相同位置。通过将键盘焦点和逻辑焦点分开,您可以实现这一点。

