Why WPF text box does not support triple-click to select all text

is there a reason why the WPF text box does not support three-click behavior like most modern user interfaces do?

By "triple-click" I mean: double-clicking on a line of text in the text box selects one word, and triple-clicking selects the entire line.

Is there an attribute that can be applied to a text box or other workaround? Is it likely that Microsoft will execute it as the default behavior for the WPF text box?

+5
source share
2 answers

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.

+8
source

You can try it using PreviewMouseDown-Event

 private void MyTextBoxPreviewMouseDown(object sender, MouseButtonEventArgs e){ if (e.ClickCount == 3) { MyTexBox.SelectAll(); } } 
+2
source

Source: https://habr.com/ru/post/1264344/


All Articles