How to select all text in TextBox WPF when focus?

前端 未结 2 1118
一整个雨季
一整个雨季 2020-12-21 17:54

I have tried the below code to select all text in textbox when focus. But this is not working.

XAML:

        

        
2条回答
  •  悲哀的现实
    2020-12-21 18:11

    in App.xaml file

    
        
    
    

    in App.xaml.cs file

    private void TextBox_GotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
    {
        TextBox tb = (TextBox)sender;
        tb.Dispatcher.BeginInvoke(new Action(() => tb.SelectAll()));
    }
    

    With this code you reach all TextBox in your Application

提交回复
热议问题