重点关注时如何在TextBox WPF中选择所有文本? [英] How to select all text in TextBox WPF when focused?
本文介绍了重点关注时如何在TextBox WPF中选择所有文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了下面的代码来在聚焦时选择文本框中的所有文本.但这是行不通的.
I have tried the below code to select all text in textbox when focus. But this is not working.
XAML :
<TextBox Text="test1" Width="100" Height="200"
GotFocus="TextBox_GotFocus"></TextBox>
c#:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).SelectAll();
//(sender as TextBox).Select(0, (sender as TextBox).Text.Length);
(sender as TextBox).Focus();
e.Handled = true;
}
我也尝试过异步.冲浪很多,但是没有用.请提出建议吗?
I have tried with asynchronous also. Surf lots , but nothing works. Please suggest?
推荐答案
您可以使用调度程序:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Dispatcher.BeginInvoke(new Action(() => textBox.SelectAll()));
}
这篇关于重点关注时如何在TextBox WPF中选择所有文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文