How to select all text in TextBox WPF when focus?

前端 未结 2 1119
一整个雨季
一整个雨季 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

    You could use the dispatcher:

    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBox = (TextBox)sender;
        textBox.Dispatcher.BeginInvoke(new Action(() => textBox.SelectAll()));
    }
    

提交回复
热议问题