Skip to content

Commit 0fc9325

Browse files
author
eriklimakc
committed
chore: Adding KeyUpCommand
1 parent c25fc7b commit 0fc9325

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ public static class InputExtensions
2727
{
2828
private static readonly ILogger _logger = typeof(InputExtensions).Log();
2929

30+
#region DependencyProperty: KeyUpCommand
31+
32+
/// <summary>
33+
/// Backing property to trigger a command when a key is released.
34+
/// </summary>
35+
public static DependencyProperty KeyUpCommandProperty { get; } = DependencyProperty.RegisterAttached(
36+
"KeyUpCommand", typeof(ICommand), typeof(InputExtensions), new PropertyMetadata(default(ICommand), OnKeyUpCommandChanged));
37+
38+
public static ICommand GetKeyUpCommand(UIElement element)
39+
=> (ICommand)element.GetValue(KeyUpCommandProperty);
40+
41+
public static void SetKeyUpCommand(UIElement element, ICommand command)
42+
=> element.SetValue(KeyUpCommandProperty, command);
43+
44+
private static void OnKeyUpCommandChanged(DependencyObject snd, DependencyPropertyChangedEventArgs args)
45+
{
46+
if (snd is UIElement elt)
47+
{
48+
elt.KeyUp -= OnKeyUp;
49+
if (args.NewValue is ICommand)
50+
{
51+
elt.KeyUp += OnKeyUp;
52+
}
53+
}
54+
}
55+
private static void OnKeyUp(object snd, KeyRoutedEventArgs e)
56+
{
57+
if (snd is UIElement elt && GetKeyUpCommand(elt) is { } command
58+
&& command.CanExecute(e.Key))
59+
{
60+
command.Execute(e.Key);
61+
}
62+
}
63+
#endregion
3064
#region DependencyProperty: AutoDismiss
3165

3266
/// <summary>

0 commit comments

Comments
 (0)