mirror of
https://github.com/VitalickS/BrightSharp.Toolkit.git
synced 2026-03-21 10:21:16 +00:00
.net copy
This commit is contained in:
33
BrightSharp.NET/Behaviors/SelectAllTextOnFocusBehavior.cs
Normal file
33
BrightSharp.NET/Behaviors/SelectAllTextOnFocusBehavior.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
using System.Windows.Controls.Primitives;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user