123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using System.Reactive.Disposables;
- using Avalonia.Input;
- using Avalonia.Interactivity;
- namespace Avalonia.Xaml.Interactions.Custom;
- /// <summary>
- ///
- /// </summary>
- public class ExecuteCommandOnKeyUpBehavior : ExecuteCommandOnKeyBehaviorBase
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="disposable"></param>
- protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
- {
- var control = SourceControl ?? AssociatedObject;
- var dispose = control?
- .AddDisposableHandler(
- InputElement.KeyUpEvent,
- OnKeyUp,
- EventRoutingStrategy);
- if (dispose is not null)
- {
- disposable.Add(dispose);
- }
- }
- private void OnKeyUp(object? sender, KeyEventArgs e)
- {
- var haveKey = Key is not null && e.Key == Key;
- var haveGesture = Gesture is not null && Gesture.Matches(e);
- if (!haveKey && !haveGesture)
- {
- return;
- }
- if (e.Handled)
- {
- return;
- }
- if (ExecuteCommand())
- {
- e.Handled = MarkAsHandled;
- }
- }
- }
|