public static class TextBoxBehavior { public static bool GetSelectAllOnFocus(DependencyObject obj) { return (bool)obj.GetValue(SelectAllOnFocusProperty); } public static void SetSelectAllOnFocus(DependencyObject obj, bool value) { obj.SetValue(SelectAllOnFocusProperty, value); } public static readonly DependencyProperty SelectAllOnFocusProperty = DependencyProperty.RegisterAttached( "SelectAllOnFocus", typeof(bool), typeof(TextBoxBehavior), new UIPropertyMetadata(false, OnSelectAllOnFocusChanged) ); private static void OnSelectAllOnFocusChanged( DependencyObject depObj, DependencyPropertyChangedEventArgs e) { TextBox textBox = depObj as TextBox; if (textBox != null && e.NewValue is bool) { if ((bool)e.NewValue) textBox.GotFocus += TextBox_SelectAllOnFocus; else textBox.GotFocus -= TextBox_SelectAllOnFocus; } } private static void TextBox_SelectAllOnFocus(object sender, RoutedEventArgs e) { (sender as TextBox).SelectAll(); } }