Created
January 28, 2016 07:54
-
-
Save eshy/5328e780c0ad41aeb51b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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