Click here to Skip to main content
13,861,006 members
Click here to Skip to main content
Add your own
alternative version


34 bookmarked
Posted 1 Apr 2009
Licenced CC (Attr 3U)

Setting the Initial Focus in WPF

, 14 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Shows how to set the initial focus in XAML

In WPF dialog boxes, I quite often see some code which looks like the following code example:

private void OnLoaded(object sender, EventArgs e)

This is usually accompanied by some XAML that looks like the following XAML example.

<Window ...


Occasionally, I also see the event being set up in the constructor. For some reason, this really gets on my nerves. I prefer to see event handlers defined in the XAML. I’m not quite sure what my objection is; I think I just prefer to see all the UI things in one place.

What this actually achieves (as you can probably work out), is that the control (in this case, a TextBox) named _textSearch has focus when the dialog box is loaded. In actual fact, WPF provides a much easier way of setting the initial focus by using the FocusManager. The FocusManager provides a set of static methods, properties, and events that you use to determine and set the focus scope[1] and to set the focused element within the focus scope.

So, using the FocusManager, you don’t need to handle the Loaded event and manually calling the Focus method as shown in the previous code examples, you can simply set the FocusedElement on the FocusManager as shown in the following code example:

<Window ...

        FocusManager.FocusedElement="{Binding ElementName=_textSearch}">

Simple, neat, all in one place, and no code-behind :)

[1] For more information about focus scopes, see FocusManager Class on the MSDN web site.

This work is licensed under a Creative Commons Attribution By license.


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License


About the Author

Derek Lakin
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

SuggestionAnother Global Solution Pin
Tom John28-Aug-13 1:21
memberTom John28-Aug-13 1:21 
GeneralDoes not work in all (common) situations Pin
Xcalllibur6-May-10 19:16
memberXcalllibur6-May-10 19:16 
GeneralRe: Does not work in all (common) situations Pin
Tom John28-Aug-13 1:14
memberTom John28-Aug-13 1:14 
GeneralRe: Does not work in all (common) situations Pin
Derek Lakin28-Aug-13 1:36
memberDerek Lakin28-Aug-13 1:36 
QuestionHow do you change focus in XAML? Pin
ldyc23-Apr-10 2:43
memberldyc23-Apr-10 2:43 
GeneralXAMS vs. Code Behind Pin
DaProgramma13-Apr-09 12:25
memberDaProgramma13-Apr-09 12:25 
GeneralRe: XAMS vs. Code Behind Pin
Derek Lakin13-Apr-09 22:50
memberDerek Lakin13-Apr-09 22:50 
GeneralRe: XAMS vs. Code Behind Pin
DaProgramma14-Apr-09 7:39
memberDaProgramma14-Apr-09 7:39 
GeneralRe: XAMS vs. Code Behind Pin
Derek Lakin14-Apr-09 7:53
memberDerek Lakin14-Apr-09 7:53 
GeneralRe: XAMS vs. Code Behind Pin
DaProgramma14-Apr-09 8:12
memberDaProgramma14-Apr-09 8:12 
GeneralRe: XAMS vs. Code Behind Pin
Derek Lakin14-Apr-09 22:28
memberDerek Lakin14-Apr-09 22:28 
GeneralThanks Pin
0xfded7-Apr-09 15:10
member0xfded7-Apr-09 15:10 
GeneralRe: Thanks Pin
Derek Lakin7-Apr-09 23:06
memberDerek Lakin7-Apr-09 23:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190214.1 | Last Updated 15 Apr 2010
Article Copyright 2009 by Derek Lakin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid