using System.Reactive.Disposables; using Avalonia.Input; using Avalonia.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; /// /// /// public class ExecuteCommandOnPointerCaptureLostBehavior : ExecuteCommandRoutedEventBehaviorBase { /// /// /// /// protected override void OnAttachedToVisualTree(CompositeDisposable disposable) { var control = SourceControl ?? AssociatedObject; var dispose = control? .AddDisposableHandler( InputElement.PointerCaptureLostEvent, OnPointerCaptureLost, EventRoutingStrategy); if (dispose is not null) { disposable.Add(dispose); } } private void OnPointerCaptureLost(object? sender, RoutedEventArgs e) { if (e.Handled) { return; } if (ExecuteCommand()) { e.Handled = MarkAsHandled; } } }