[WPF] achieve TextBox text box, click Select All

Original: [WPF] achieve TextBox text box, click Select All

        /// <Summary>
        /// Void: Select the text provided when acquiring focus
        /// </ Summary>
        /// <param name = "TextBox"> the textbox </ param>
        public void SetSelectionAllOnGotFocus (the TextBox TextBox)
        {

            MouseButtonEventHandler _OnPreviewMouseDown = (sender, e) =>
            {
                TextBox box = e.Source as TextBox;
                box.Focus();
                e.Handled = true;
            };
            RoutedEventHandler _OnLostFocus = (sender, e) =>
            {
                TextBox box = e.Source as TextBox;
                box.PreviewMouseDown += _OnPreviewMouseDown;
            };
            RoutedEventHandler _OnGotFocus = (sender, e) =>
            {
                TextBox box = e.Source as TextBox;
                box.SelectAll();
                box.PreviewMouseDown -= _OnPreviewMouseDown;
            };

            textbox.PreviewMouseDown += _OnPreviewMouseDown;
            textbox.LostFocus += _OnLostFocus;
            textbox.GotFocus += _OnGotFocus;
        }
 

Released three original articles · won praise 0 · Views 969

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12075360.html