is there a reason why a WPF textbox does not support triple-click behaviour like most modern UIs these days do?
By "triple click", I mean: double clicking a line of text in a textbox selects a single word, while triple clicking selects the whole line.
Is there an attribute that can be applied to textbox, or another workaround? Is there any chance Microsoft will implement it as default behaviour for a WPF textbox?
The TextBox control provides support for basic text input in WPF applications. Gets or sets the style used by this element when it is rendered. Represents a control that provides a scroll bar that has a sliding Thumb whose position corresponds to a value.
Triple click feels more like a shortcut in the Shortcuts/Intents/Actions triad. When I double click somewhere, my intent may be to select that word. If I triple click, my intent may be to select the line. Or it may be to select the paragraph.
On native platforms and browsers using a triple click (with a mouse) on text in a textfield one selects all text in the textfield. Flutter doesn't do this, but rather deselects the selected word. Flutter should support this to match native platforms and browsers.
In this example, a button with an associated Clickevent handler is used to retrieve the text selection. When the user clicks the button, the OnClickmethod copies any selected text in the textbox into a string.
As already answered, you can implement that manually. But you don't want to do that for each textbox in your application. So you can implement attached property and set it in style, like this:
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 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 textboxes will automatically have this triple-click behavior.
You can try it by using PreviewMouseDown-Event
private void MyTextBoxPreviewMouseDown(object sender, MouseButtonEventArgs e){
if (e.ClickCount == 3) {
MyTexBox.SelectAll();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With