/// <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;
}
发布了3 篇原创文章 · 获赞 0 · 访问量 969
原文地址:https://www.cnblogs.com/lonelyxmas/p/12075360.html
时间: 2024-11-05 21:38:33