0
votes

In researching a problem I was having with Focus (in WPF), I came across this description in FocusManager Class. I'm afraid paragraphs 5 and 6 lost me.

Can someone explain in simple terms the last two paragraphs?

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.

1

1 Answers

4
votes

In short there may be several different focus scopes in an application or a view but there is only one element on the entire screen that can have the keyboard focus.

In each focus scope there may be at most one element that has logical focus. So if you have let's say 4 focus scopes in your view, you may have up to 4 elements in total that have logical focus but only one of these may have keyboard focus.

Keyboard focus refers to the element that is currently receiving keyboard input. When keyboard focus leaves a specific focus scope, the focused element will lose keyboard focus but will retain logical focus. This means that when keyboard focus returns to the focus scope, the focused element will obtain keyboard focus again.

So if you have two elements in focus scope A, for example a TextBox and Button, and one of them has keyboard focus when you put the cursor in another TextBox in another focus scope B, the TextBox is scope A still has the logical focus in focus scope A while the TextBox in focus scope B has keyboard focus.

Hope that makes sense.