Skip to content

Commit

Permalink
chore: Adding KeyUpCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Feb 16, 2023
1 parent c25fc7b commit 0fc9325
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Uno.Toolkit.UI/Behaviors/InputExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ public static class InputExtensions
{
private static readonly ILogger _logger = typeof(InputExtensions).Log();

#region DependencyProperty: KeyUpCommand

/// <summary>
/// Backing property to trigger a command when a key is released.
/// </summary>
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

/// <summary>
Expand Down

0 comments on commit 0fc9325

Please sign in to comment.