/// <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;
}