For once I´ll ramble less and provide more 🙂

A simple class to help you make the WPF TextBox able to select the text when it gets focus. I´ve gotten tired of subclassing all the time so this is a neat little class that you can just attach to any TextBox-control.

The class looks like this:

public static class TextExt {
    public static readonly DependencyProperty SelectAllProperty =
        DependencyProperty.RegisterAttached ("SelectAll", typeof (bool), typeof (TextExt), new PropertyMetadata (default (bool), OnSelectAllChanged));

    /// <summary>Handle when the <see cref="SelectAllProperty"/> changes for an element</summary>
    private static void OnSelectAllChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) {
        // Make sure the object is a TextBox
        var textBox = d as TextBox;
        if (textBox == null)
            throw new InvalidOperationException (
                "SelectAll property can only be assigned to elements of type TextBox");

        // Unassign previous events
        textBox.GotKeyboardFocus -= GotKeyboardFocus;
        textBox.GotMouseCapture -= GotMouseFocus;

        // If property is set to true, reassign events again
        if (!((bool) e.NewValue))

        textBox.GotKeyboardFocus += GotKeyboardFocus;
        textBox.GotMouseCapture += GotMouseFocus;

    private static void SelectAll (object sender) {
        ((TextBox) sender).SelectAll ();

    private static void GotKeyboardFocus (object sender, KeyboardFocusChangedEventArgs e) {
        SelectAll (sender);

    private static void GotMouseFocus (object sender, MouseEventArgs mouseEventArgs) {
        SelectAll (sender);

    public static void SetSelectAll (TextBox element, bool value) {
        element.SetValue (SelectAllProperty, value);

    public static bool GetSelectAll (TextBox element) {
        return (bool) element.GetValue (SelectAllProperty);

And you use it like this:

<TextBox Text="{Binding Weight}" XamlExtensions:TextExt.SelectAll="True" />

Seems to be working pretty well and with a small amount of code.