This is a migrated thread and some comments may be shown as answers.

Select all on double click

2 Answers 661 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Karl B
Top achievements
Rank 1
Karl B asked on 19 Jan 2018, 10:00 PM
If I have a string value that represents a decimal number (e.g. "15.123") the current default double click behavior is to select only the text before or after the decimal.  How can I change this to select all text in the field on double click?

2 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 24 Jan 2018, 12:42 PM
Hi Karl,

A possible approach which you can try is to use the EventManager.RegisterClassHandler() method to subscribe to the MouseDoubleClickEvent of the TextBoxes. In the event handler, you can get the TextBox and called its SelectAll() method which selects all the text. Check the following code snippet.
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.MouseDoubleClickEvent, new RoutedEventHandler(OnMouseDoubleClickEvent));
    }
 
    private void OnMouseDoubleClickEvent(object sender, RoutedEventArgs e)
    {
        (sender as TextBox).SelectAll();
    }
}

Give this approach a try and let me know if it works in your main application.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Karl B
Top achievements
Rank 1
answered on 26 Jan 2018, 09:47 PM

While the EventManager solution appears to work, I was concerned about using it in our application after reading about possible memory leak issues if used incorrectly.

I then realized we can achieve this with a style EventSetter:

<Style TargetType="{x:Type TextBox}">
    <EventSetter Event="MouseDoubleClick" Handler="EventSetter_OnHandler"/>
</Style>
private void EventSetter_OnHandler (object sender, MouseButtonEventArgs e)
      {
      if ( sender is TextBox )
          {
          (sender as TextBox).SelectAll();
          }
      }

 

Tags
PropertyGrid
Asked by
Karl B
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Karl B
Top achievements
Rank 1
Share this question
or