From 0fc9325082e01cb4e24c14583172f7f3fda6e2b9 Mon Sep 17 00:00:00 2001 From: eriklimakc Date: Mon, 30 Jan 2023 11:23:52 +0000 Subject: [PATCH] chore: Adding KeyUpCommand --- .../Behaviors/InputExtensions.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs b/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs index 44f1f9f7b..7e8feb7ef 100644 --- a/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs +++ b/src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs @@ -27,6 +27,40 @@ public static class InputExtensions { private static readonly ILogger _logger = typeof(InputExtensions).Log(); + #region DependencyProperty: KeyUpCommand + + /// + /// Backing property to trigger a command when a key is released. + /// + public static DependencyProperty KeyUpCommandProperty { get; } = DependencyProperty.RegisterAttached( + "KeyUpCommand", typeof(ICommand), typeof(InputExtensions), new PropertyMetadata(default(ICommand), OnKeyUpCommandChanged)); + + public static ICommand GetKeyUpCommand(UIElement element) + => (ICommand)element.GetValue(KeyUpCommandProperty); + + public static void SetKeyUpCommand(UIElement element, ICommand command) + => element.SetValue(KeyUpCommandProperty, command); + + private static void OnKeyUpCommandChanged(DependencyObject snd, DependencyPropertyChangedEventArgs args) + { + if (snd is UIElement elt) + { + elt.KeyUp -= OnKeyUp; + if (args.NewValue is ICommand) + { + elt.KeyUp += OnKeyUp; + } + } + } + private static void OnKeyUp(object snd, KeyRoutedEventArgs e) + { + if (snd is UIElement elt && GetKeyUpCommand(elt) is { } command + && command.CanExecute(e.Key)) + { + command.Execute(e.Key); + } + } + #endregion #region DependencyProperty: AutoDismiss ///