逻辑焦点和键盘焦点有什么区别?

What is the difference between logical focus and keyboard focus?

在研究我在使用 Focus(在 WPF 中)时遇到的问题,我在 FocusManager Class 中看到了这个描述。恐怕第 5 段和第 6 段让我迷路了。

谁能简单解释一下最后两段?

TIA

In Windows Presentation Foundation (WPF) there are two concepts concerning focus: keyboard focus and logical focus.

Keyboard focus pertains to the element which is currently receiving keyboard input. There can be only one element with keyboard focus. This element with keyboard focus has IsKeyboardFocused set to true. Keyboard.FocusedElement returns the element with keyboard focus.

Logical focus pertains to the FocusManager.FocusedElement within a specific focus scope.

A focus scope is a container element that keeps track of the FocusManager.FocusedElement within its scope. By default, the Window class is a focus scope as are the Menu, ContextMenu, and ToolBar classes. An element which is a focus scope has IsFocusScope set to true.

There can be multiple elements with logical focus, but there can only be one element with logical focus within a single focus scope. An element with logical focus does not necessarily have keyboard focus, but an element with keyboard focus will have logical focus. It is possible to define a focus scope within a focus scope. In this case, both the parent focus scope and the child focus scope can have a FocusManager.FocusedElement.

The following scenario illustrates how keyboard focus and logical focus change in a Windows Presentation Foundation (WPF) application that has a Window with a TextBox and a Menu which has a MenuItem. When keyboard focus changes from the TextBox to the MenuItem, the TextBox losses keyboard focus but retains logical focus for the Window focus scope. The MenuItem obtains keyboard focus and obtains logical focus for the Menu focus scope. When keyboard focus returns to the root Window, the element in Window focus scope with logical focus will obtain keyboard focus, which in this case is the TextBox. The TextBox now has keyboard focus and logical focus. The MenuItem loses keyboard focus, but retains logical focus for the Menu focus scope.

简而言之,一个应用程序或一个视图中可能有多个不同的焦点范围,但整个屏幕上只有一个元素可以拥有键盘焦点。

在每个焦点范围中,最多可以有一个元素具有 逻辑 焦点。因此,如果您在视图中假设有 4 个焦点范围,则总共可能有多达 4 个具有 logical 焦点的元素,但其中只有一个可能具有 keyboard重点。

键盘焦点是指当前正在接受键盘输入的元素。当键盘焦点离开特定的焦点范围时,获得焦点的元素将失去键盘焦点,但会保留逻辑焦点。这意味着当键盘焦点returns到焦点范围时,焦点元素将重新获得键盘焦点。

因此,如果您在焦点范围 A 中有两个元素,例如 TextBox 和 Button,并且当您将光标放在另一个焦点范围 B 中的另一个 TextBox 中时,其中一个具有键盘焦点,则 TextBox 仍然是范围 A在焦点范围 A 中具有逻辑焦点,而在焦点范围 B 中的 TextBox 具有键盘焦点。

希望这是有道理的。