KeyBinding.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Windows.Input;
  2. namespace Avalonia.Input
  3. {
  4. public class KeyBinding : AvaloniaObject
  5. {
  6. public static readonly StyledProperty<ICommand> CommandProperty =
  7. AvaloniaProperty.Register<KeyBinding, ICommand>(nameof(Command));
  8. public ICommand Command
  9. {
  10. get { return GetValue(CommandProperty); }
  11. set { SetValue(CommandProperty, value); }
  12. }
  13. public static readonly StyledProperty<object> CommandParameterProperty =
  14. AvaloniaProperty.Register<KeyBinding, object>(nameof(CommandParameter));
  15. public object CommandParameter
  16. {
  17. get { return GetValue(CommandParameterProperty); }
  18. set { SetValue(CommandParameterProperty, value); }
  19. }
  20. public static readonly StyledProperty<KeyGesture> GestureProperty =
  21. AvaloniaProperty.Register<KeyBinding, KeyGesture>(nameof(Gesture));
  22. public KeyGesture Gesture
  23. {
  24. get { return GetValue(GestureProperty); }
  25. set { SetValue(GestureProperty, value); }
  26. }
  27. public void TryHandle(KeyEventArgs args)
  28. {
  29. if (Gesture?.Matches(args) == true)
  30. {
  31. args.Handled = true;
  32. if (Command?.CanExecute(CommandParameter) == true)
  33. Command.Execute(CommandParameter);
  34. }
  35. }
  36. }
  37. }