Files
Toolkit2/Toolkit.UI.Avalonia/KeyBindingBehavior.cs
T
2024-04-22 22:30:57 +01:00

39 lines
978 B
C#

using Avalonia;
using Avalonia.Input;
using Avalonia.Xaml.Interactivity;
using System.Windows.Input;
namespace Toolkit.UI.Avalonia;
public class KeyBindingBehavior :
Trigger<InputElement>,
ICommand
{
public static readonly StyledProperty<KeyBinding> KeyBindingProperty =
AvaloniaProperty.Register<KeyBindingBehavior, KeyBinding>(nameof(KeyBinding));
public KeyBinding KeyBinding
{
get => GetValue(KeyBindingProperty);
set => SetValue(KeyBindingProperty, value);
}
public event EventHandler? CanExecuteChanged;
protected override void OnAttached()
{
if (KeyBinding != null)
{
KeyBinding.Command = this;
AssociatedObject?.KeyBindings.Add(KeyBinding);
}
base.OnAttached();
}
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) =>
Interaction.ExecuteActions(AssociatedObject, Actions, null);
}