聚焦时如何选择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屋!
查看全文