Joe White’s Blog

Life, .NET, and Cats


WPF design flaw of the day: no initial focus

When you write a WPF app and start it up, nothing has focus.

I’d call this a bug, but Microsoft has scads of testers. Somebody had to have noticed. So I can only conclude that somebody decided, on purpose, that when you write a WPF app and run it, the user shouldn’t be able to just start typing.


This is completely unlike the other two major GUI frameworks I’ve used (Delphi VCL and Windows Forms), where the app is usable by default: you open a window, your focus is in the first control in the tab order. I was therefore rather grumpy when I found out about the WPF brokenness.

Fortunately, after some digging around in Reflector, I found a nice, general fix. Adding this to the window’s constructor:

Loaded += (sender, e) =>
    MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

will re-enable the standard behavior: initial focus is in the first control in the tab order.

This line could easily be pulled into a base class, so that all your windows can automatically inherit this behavior.

One Response to “WPF design flaw of the day: no initial focus”

  1. Dew Drop - May 4, 2009 | Alvin Ashcraft's Morning Dew Says:

    [...] WPF design flaw of the day: no initial focus (Joe White) [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Joe White's Blog copyright © 2004-2011. Portions of the site layout use Yahoo! YUI Reset, Fonts, and Grids.
Proudly powered by WordPress. Entries (RSS) and Comments (RSS). Privacy policy