2021-04-14 15:28:30 +03:00
|
|
|
|
using Microsoft.Xaml.Behaviors;
|
|
|
|
|
|
using System.Windows.Controls.Primitives;
|
2017-08-19 15:10:31 +03:00
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BrightSharp.Behaviors
|
|
|
|
|
|
{
|
|
|
|
|
|
public class SelectAllTextOnFocusBehavior : Behavior<TextBoxBase>
|
|
|
|
|
|
{
|
|
|
|
|
|
protected override void OnAttached() {
|
|
|
|
|
|
base.OnAttached();
|
|
|
|
|
|
AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus;
|
|
|
|
|
|
AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnDetaching() {
|
|
|
|
|
|
base.OnDetaching();
|
|
|
|
|
|
AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus;
|
|
|
|
|
|
AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AssociatedObjectGotKeyboardFocus(object sender,
|
|
|
|
|
|
KeyboardFocusChangedEventArgs e) {
|
|
|
|
|
|
AssociatedObject.SelectAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
|
|
|
|
|
|
if (!AssociatedObject.IsKeyboardFocusWithin) {
|
|
|
|
|
|
AssociatedObject.Focus();
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|