Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Add ExecuteCommandOnLostFocusBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Apr 24, 2024
1 parent 886d60b commit 218ec6b
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Reactive.Disposables;
using Avalonia.Input;
using Avalonia.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;

/// <summary>
///
/// </summary>
public class ExecuteCommandOnLostFocusBehavior : ExecuteCommandBehaviorBase
{
/// <summary>
///
/// </summary>
/// <param name="disposable"></param>
protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
{
var dispose = AssociatedObject?
.AddDisposableHandler(
InputElement.LostFocusEvent,
AssociatedObject_LostFocus,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);

if (dispose is not null)
{
disposable.Add(dispose);
}
}

private void AssociatedObject_LostFocus(object? sender, RoutedEventArgs e)
{
if (e.Handled)
{
return;
}

if (ExecuteCommand())
{
e.Handled = true;
}
}
}

0 comments on commit 218ec6b

Please sign in to comment.