How to automatically select all text on focus in WPF TextBox?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Selecting all text when a WPF TextBox receives focus is a small UX improvement that makes data entry much faster, especially for forms where users often replace the existing value. The tricky part is that keyboard focus and mouse focus behave differently, so a good solution handles both.
The basic behavior with focus events
The core action is simple: when the TextBox gets keyboard focus, call SelectAll():
If users tab into the field, this works well. The problem shows up with the mouse. A normal mouse click both gives focus and sets the caret, so the selection can be lost immediately unless you intercept the mouse event.
Handle mouse focus correctly
To make the first click focus the box and select the text instead of placing the caret, handle PreviewMouseLeftButtonDown:
The key condition is !textBox.IsKeyboardFocusWithin. If the box does not yet have focus, the handler grabs focus on the first click and prevents the default caret placement. After focus is established, the GotKeyboardFocus handler selects the text.
Wired together in XAML, the solution looks like this:
This is enough for many applications and is easy to understand during maintenance.
A reusable attached behavior
If you need this behavior across many text boxes, an attached property is cleaner than repeating event handlers in every window or user control.
Then enable it in XAML:
This keeps the view logic declarative and avoids repetitive code-behind wiring.
Why GotFocus alone is usually not enough
Many examples on the internet use only GotFocus or GotKeyboardFocus. That works for keyboard navigation, but mouse clicks often still place the caret where the user clicked. The result is inconsistent behavior: tabbing selects all, clicking does not.
That is why the preview mouse event matters. It changes the first click from "place caret here" into "give focus first", which allows the focus handler to own the selection.
Common Pitfalls
The most common mistake is using only GotFocus and then wondering why single-click selection fails. Mouse focus and keyboard focus are not the same interaction path.
Another issue is attaching the preview mouse handler too broadly. If you apply it to read-only or specialized text controls without thinking through the interaction, you may create surprising behavior.
Developers also sometimes reselect text every time the field is clicked, which makes it impossible to place the caret intentionally after the first focus event. The IsKeyboardFocusWithin check prevents that.
Finally, test with tab navigation, mouse clicks, and programmatic focus changes. Good focus behavior should feel consistent across all three.
Summary
- Call
SelectAll()when theTextBoxreceives keyboard focus. - Intercept the first mouse click with
PreviewMouseLeftButtonDownso the caret does not override the selection. - Use an attached behavior when you want the pattern across many text boxes.
- '
GotFocusalone is usually not enough for a polished experience.' - Guard the mouse logic with
IsKeyboardFocusWithinso later clicks still behave naturally.
Related reading
- How to await an IAsyncAction method?
- How to bind an enum to a combobox control in WPF?
- How to build a query string for a URL in C?
- How to bust the cache or obtain cache key when using <distributed-cache> Tag helper in Asp.net Core MVC
- How to calculate pi to N number of places in C using loops
- How to calculate simple moving average faster in C?
- How to call async methods from within a Prism event aggregator subscriber?
- How to call asynchronous method from synchronous method in C#?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.