The focus of WPF

Focus in WPF

There are two types of focus in WPF: keyboard focus and logical focus .
Many people do not understand at first, why are there two focal points?
What is the difference between these two focal points?
Which one should I use?
Today I will explain the focus in WPF. Well, you will suddenly see the common applications . Hahaha.

1. Conclusion

Let me start with the conclusion: the keyboard focus is the true focus, and the logical focus is the false focus.

We understand that getting focus means: after a control gets the focus, it will be active.
Keyboard focus is what we usually call true focus. When the control gets keyboard focus, it will be active.
But when the control gains logical focus, it is not guaranteed to be active.

2. Concept

1. Keyboard focus

The keyboard focus is the focus of our common sense.
When a control gains keyboard focus , the control will be active.
Only one on the entire desktop keyboard focus , to get the keyboard focus element can accept keyboard input.

2. Logical focus and focus range

Logical focus: used to record the position of the keyboard focus in the focus range.
Focus range: The range where the logical focus can be moved, and the logical focus cannot leave the focus range.

When the keyboard focus leaves the focus range , the logical focus will not follow the keyboard focus to leave, but remains at the position when it left. When the keyboard focus returns to this focus range , it will first return to the position of the logical focus .

Three, common applications

The application of logical focus is the most intuitive to interact with multiple windows.

Whenever you are ready to enter content in an input box on a web page, switch to another web page or software, and then return to the previous web page, you will find that your cursor is still in this input box. Why can he remember the position before your cursor? We always take it for granted that this should be the case. In fact, this is because the logical focus is retained in the form .
You can open two webpages directly, then switch to see. It's simple.

When the keyboard focus returns to the focus range , the control where the logical focus is located first gets the keyboard focus .
Through this example, I believe you can understand these three concepts in an instant.

Four, focus management

FcousManager class

FocusManagerThe class provides two additional properties for focus management.

1.FocusedElement

Specify a control, when the screen is opened, this control automatically gets the keyboard focus.

2.IsFocusScope

Whether the control setting this additional property establishes the focus range.
If set to True, a focus range is formed in this control. When the focus leaves this range, the internal child controls will retain the logical focus.

And some ways to manage focus:

FcousManager.GetFocusScope()方法

Use this method to return to the most recent focus range.

FocusManager.GetIsFocus() method

Specify whether the control is the focus range.

FocusManager.SetFocused()方法

Give a control logical focus.

Note: The prerequisite for getting focus is that the Focusable property is True.

Other ways to manage focus

Keyboard.Focus() method

Try to get keyboard focus for the specified control

Control.Focus() method

Try to get keyboard focus for the specified control

IsFocused

Check whether the logical focus is obtained

IsKeyboardFocused

Check whether the keyboard focus is obtained

Does it help you? Like it~

Guess you like

Origin blog.csdn.net/qq_42068856/article/details/103097137