[WPF]实现TextBox文本框单击全选
Posted lonelyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[WPF]实现TextBox文本框单击全选相关的知识,希望对你有一定的参考价值。
原文:[WPF]实现TextBox文本框单击全选
/// <summary>
/// Void:设置获取焦点时全选文本
/// </summary>
/// <param name="textbox">指定文本框</param>
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]实现TextBox文本框单击全选的主要内容,如果未能解决你的问题,请参考以下文章