As already mentioned, you can implement this manually. But you do not want to do this for every text field in your application. Thus, you can implement the attached property and set it in style, for example:
public static class TextBoxBehavior { public static readonly DependencyProperty TripleClickSelectAllProperty = DependencyProperty.RegisterAttached( "TripleClickSelectAll", typeof(bool), typeof(TextBoxBehavior), new PropertyMetadata(false, OnPropertyChanged)); private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var tb = d as TextBox; if (tb != null) { var enable = (bool)e.NewValue; if (enable) { tb.PreviewMouseLeftButtonDown += OnTextBoxMouseDown; } else { tb.PreviewMouseLeftButtonDown -= OnTextBoxMouseDown; } } } private static void OnTextBoxMouseDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount == 3) { ((TextBox)sender).SelectAll(); } } public static void SetTripleClickSelectAll(DependencyObject element, bool value) { element.SetValue(TripleClickSelectAllProperty, value); } public static bool GetTripleClickSelectAll(DependencyObject element) { return (bool) element.GetValue(TripleClickSelectAllProperty); } }
Then create the style somewhere (for example, in application resources in App.xaml):
<Application.Resources> <Style TargetType="TextBox"> <Setter Property="local:TextBoxBehavior.TripleClickSelectAll" Value="True" /> </Style> </Application.Resources>
Now all your text fields will automatically have this behavior with three clicks.
source share