的GotFocus和GotKeyboardFocus之间的差 [英] The difference between GotFocus and GotKeyboardFocus

查看:661
本文介绍了的GotFocus和GotKeyboardFocus之间的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是的GotFocus GotKeyboardFocus -and同样引发LostFocus之间的差值(S) LostKeyboardFocus

What is the difference(s) between GotFocus and GotKeyboardFocus -and similarly LostFocus and LostKeyboardFocus?

对不起,我简单的问题,但是,我GOOGLE了它,并阅读了大量的博客文章,但我仍然感到困惑。似乎没有人知道到底是什么区别):

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 ):

更新:

我的用法:

我创建通过延长控制类的自定义控件。类似组合框但也有一些其他的影响。我试图打开和关闭弹出通过设置一个属性: IsDropDownOpen 就像组合框通过的GotFocus 引发LostFocus 事件。我不想弹出关闭掉,当我 Alt + Tab键 ED窗户,但是当我关闭点击一个按钮例如或者我去一个文本框。我所做的:

I am creating a custom control by extending Control class. Something like ComboBox but with some other effects. I'm trying to open and close a Popup by setting a property: IsDropDownOpen just like a ComboBox through the GotFocus and LostFocus events. I don't want to Popup get closed, when I Alt+Tabed the windows, but get closed when I click on a Button for example or I go to a TextBox. I did:

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;
}



的GotFocus 作品。但忘记不满意。如果我做了忘记的东西,在 LostKeyboardFocus 然后当我 Alt + Tab键窗户,或窗口进入未激活,则该方法被调用,而我不想。我该如何解决呢?

The GotFocus works. But the Lost one didn't. If I do the Lost stuff in LostKeyboardFocus then when I Alt+Tab the windows, or Window goes to inactive, then the method get called, while I don't want. How can I solve it?

推荐答案

MSDN 有重点的概述,但我会尽力在这里解释它。

MSDN has an overview of focus, but I'll try to explain it here.

WPF有关于焦点2概念。有物理键盘焦点,有逻辑焦点。只有一个元素可以有键盘焦点(如果应用程序是不活动的应用程序,没有元素将有键盘焦点)。

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).

多个项目可以有逻辑焦点。事实上,你可以创建新的重点范围。根据MSDN:

Multiple items can have logical focus. In fact, you can create new "focus scopes". As per 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.

您可以一个元素(在定义自己的焦点范围一般为面板 )通过设置 FocusManager.IsFocusScope =真。这是重点范围在WPF控件默认情况下是窗口菜单项工具栏文本菜单

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.

这是有道理的,如果你想拥有多个窗口在你的应用程序。当你使用Alt-Tab 之间,你希望你的键盘焦点返回到同一个地方这是最后一次了窗口有专注。通过保持键盘焦点和逻辑焦点分开,就可以实现这一点。

This makes sense if you think about having multiple Windows in your application. When you Alt-Tab between them, you expect your keyboard focus to return to the same place it was the last time the Window had focus. By keeping keyboard focus and logical focus separate, you can achieve this.

这篇关于的GotFocus和GotKeyboardFocus之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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