using Microsoft.Xaml.Behaviors; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Input; namespace UndoTree; internal class CommitOnEnterBehavior : Behavior { protected override void OnAttached() { base.OnAttached(); AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown; } private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e) { var binding = BindingOperations.GetBindingExpression(AssociatedObject, TextBox.TextProperty); switch (e.Key) { case Key.Enter: binding?.UpdateSource(); e.Handled = true; break; case Key.Escape: binding?.UpdateTarget(); e.Handled = true; break; } } }