Focus Manager In WPF

Introduction

In this article we will see how we can manage Focus of specific controls in WPF. In some projects requirement some controls need not be focused, need to be focused all the time. We will see how to achieve that.

Crating WPF Application Project

Fire up Visual Studio 2008 and Create a WPF Application and name the project as FocusManagerWPF.

FocusManagerWPF

Let's have a bunch of controls so that we can see the real Focus Manager working.

Focus Manager

As you see above I have some Container controls that holds multiple controls inside it.

Now Let's run the application and see how default Focus works.

Default Focus

To go to the particular control we need to press TAB, and that's how we set focus on each control.

If you press TAB continuously, you will see that each control gets focused when we press tab in a cyclic manner, that is starting from Check Box 1 to the PasswordBox Control and it cycles continuously.

So we got to know that the default Focus Manager is Continuous.

Now we will see how we can restrict that.

KeyBoardNavigation

As you see above we have a Class called KeyBoardNavigation.

KeyBoardNavigation

As you see we have several properties that would help us for our tab/focus restriction.

Now choose TabNavigation.

TabNavigation

As you see above we have the following enums that we can use.

Let's see how Cycle will help us.

Tab Navigation Cycles

You would see the Tab Navigation Cycles through the CheckBoxes.

Let's see how None will help.

CheckBoxes

You would see the TAB key never focuses the CheckBoxes at all.

Let's see how Once will help us.

CheckBoxes

You would see it focuses the first element when we press TAB and continues to other controls.

The other options such as Continue, Cycle and Local are same.

So in this article we experienced how we can take help of KeyboardTabnavigation to help us for managing our Focus on specific controls.

Hope this article helps.