The title of this post is almost pure Google-juice, because this is a trick that I had to relearn on the weekend after a series of unsuccessful searches on the topic.

A behaviour in Windows that most of us are fairly accustomed to (whether we realise it or not) is that upon entering a TextBox, the text is automatically selected. This means that you can tab into a TextBox and then start typing, and whatever was already in there will be automatically overwritten.

In WPF, this behaviour is not present. When you tab into a TextBox your cursor sits at the beginning of the text, and when you start typing, your new text is simply inserted at the beginning. This is really weird for someone who’s used to the “select all on focus” behaviour, and time-consuming for a beginner (who inevitably uses their right-arrow key to scroll to the end of the text, and backspaces to erase it all).

So how do you go about getting this behaviour back? Your first instinct might be to simply catch the GotFocus event on each TextBox and call the SelectAll() method, but that’ll get pretty old by about the thirtieth time you’ve done it. Instead, here’s a simple way to register a global event handler that works on any TextBox in your application:

First, open up your App.xaml.cs file. That’s the file in a WPF project that serves as the entry point for your application. The “App” class has an overridable method called “OnStartup” from which we’ll register our event handler:

protected override void OnStartup(StartupEventArgs e)
        new RoutedEventHandler(TextBox_GotFocus));


So I’m using the EventManager class to register a global event handler (not to be confused with Evan Handler) against a type (TextBox)! Pretty neat, huh? The actual handler is dead simple:

private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    (sender as TextBox).SelectAll();

As you’d expect, we’re selecting the text in the supplied TextBox.

You can read more about class-handling of events on MSDN.