[WPF]实现TextBox文本框单击全选
2021-01-23 06:15
///
/// Void:设置获取焦点时全选文本
///
/// 指定文本框
public void SetSelectionAllOnGotFocus(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;
}
上一篇:WPF 精修篇 数据绑定到对象
下一篇:Java-多线程
文章标题:[WPF]实现TextBox文本框单击全选
文章链接:http://soscw.com/index.php/essay/45788.html