Skip to content

Instantly share code, notes, and snippets.

@eshy
Created January 28, 2016 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eshy/5328e780c0ad41aeb51b to your computer and use it in GitHub Desktop.
Save eshy/5328e780c0ad41aeb51b to your computer and use it in GitHub Desktop.
[ContentProperty(Name = nameof(Actions))]
[TypeConstraint(typeof(Control))]
public class KeyUpBehavior : DependencyObject, IBehavior
{
private Control AssociatedControl => AssociatedObject as Control;
public DependencyObject AssociatedObject { get; private set; }
public VirtualKey Key { get; set; }
public void Attach(DependencyObject associatedObject)
{
AssociatedObject = associatedObject;
AssociatedControl.KeyUp += AssociatedControl_KeyUp;
}
public void Detach()
{
AssociatedControl.KeyUp -= AssociatedControl_KeyUp;
}
private void AssociatedControl_KeyUp(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Key)
{
Interaction.ExecuteActions(AssociatedObject, Actions, null);
e.Handled = true;
}
}
public ActionCollection Actions
{
get
{
var actions = (ActionCollection)GetValue(ActionsProperty);
if (actions == null)
{
SetValue(ActionsProperty, actions = new ActionCollection());
}
return actions;
}
}
public static readonly DependencyProperty ActionsProperty =
DependencyProperty.Register("Actions", typeof(ActionCollection),
typeof(KeyUpBehavior), new PropertyMetadata(null));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment