Bialgous回答:
IInputElement focusedElement = FocusManager.GetFocusedElement(thisWindow);
有两点需要注意:
1. 逻辑焦点 != 键盘焦点
比如当你点开一个菜单时,菜单具有物理焦点
2. 要想检查得到的这个元素是否有键盘焦点,需检查 IsKeyboardFocused 属性,如果它 == true,则这个元素有键盘焦点
注:逻辑焦点、物理焦点的概念是什么?不很清楚,请大家赐教:) ....
以下是原文:
Hi, you can always determine which element has logical focus in your application through the FocusManager.GetFocusedElement method -- pass it the window in question and it will return which element has logical focus in that window. Remember that logical focus != keyboard focus at all times -- toolbars and menus track their own focus so if you are currently interacting with a menu then the menu has physical focus. But in general, the following code will tell you which element WPF thinks has focus in the window:
IInputElement focusedElement = FocusManager.GetFocusedElement(thisWindow);
To determine whether this element has keyboard focus, we can check the IsKeyboardFocused property - if it's set to true, then that element currently has the keyboard focus (as well as being the logical focus for that focus scope).