33 行
824 B
C#
33 行
824 B
C#
using Microsoft.Xaml.Behaviors;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
namespace UndoTree;
|
|
|
|
internal class FocusWhenVisibleBehavior : Behavior<TextBox>
|
|
{
|
|
protected override void OnAttached()
|
|
{
|
|
base.OnAttached();
|
|
AssociatedObject.IsVisibleChanged += AssociatedObject_IsVisibleChanged;
|
|
}
|
|
|
|
protected override void OnDetaching()
|
|
{
|
|
base.OnDetaching();
|
|
AssociatedObject.IsVisibleChanged -= AssociatedObject_IsVisibleChanged;
|
|
}
|
|
|
|
private void AssociatedObject_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
if (e.NewValue is not true)
|
|
return;
|
|
|
|
AssociatedObject.Dispatcher.BeginInvoke(() =>
|
|
{
|
|
AssociatedObject.Focus();
|
|
AssociatedObject.SelectAll();
|
|
});
|
|
}
|
|
}
|