Click here to Skip to main content
Licence CC (Attr 3U)
First Posted 1 Apr 2009
Views 23,840
Bookmarked 28 times

Setting the Initial Focus in WPF

By | 14 Apr 2010 | Technical Blog
Shows how to set the initial focus in XAML
A Technical Blog article. View original blog here.[^]

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._textSearch.Focus();
}

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

<Window ...
        Loaded="OnLoaded">

Occasionally, I also see the event being setup up in the constructor. For some reason this really gets on my nerves. I much 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.

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDoes not work in all (common) situations Pinmember=Xc@libur=18:16 6 May '10  
QuestionHow do you change focus in XAML? Pinmemberldyc1:43 23 Apr '10  
GeneralXAMS vs. Code Behind PinmemberDaProgramma11:25 13 Apr '09  
GeneralRe: XAMS vs. Code Behind PinmemberDerek Lakin21:50 13 Apr '09  
GeneralRe: XAMS vs. Code Behind PinmemberDaProgramma6:39 14 Apr '09  
GeneralRe: XAMS vs. Code Behind PinmemberDerek Lakin6:53 14 Apr '09  
GeneralRe: XAMS vs. Code Behind PinmemberDaProgramma7:12 14 Apr '09  
GeneralRe: XAMS vs. Code Behind PinmemberDerek Lakin21:28 14 Apr '09  
GeneralThanks Pinmember0xfded14:10 7 Apr '09  
GeneralRe: Thanks PinmemberDerek Lakin22:06 7 Apr '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 15 Apr 2010
Article Copyright 2009 by Derek Lakin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid