Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Attached.Behaviors.General
- {
- using System.Windows.Input;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Input;
- public class ElementTapGesture
- {
- public static readonly DependencyProperty TapItProperty =
- DependencyProperty.RegisterAttached(
- "TapCommand",
- typeof(ICommand),
- typeof(ElementTapGesture), // Changed from UIElement to this class, and now the code runs fine
- new PropertyMetadata(null, TapChanged));
- public static void SetTapCommand(DependencyObject element, ICommand value)
- {
- element.SetValue(TapItProperty, value);
- }
- public static ICommand GetTapCommand(DependencyObject element)
- {
- return (ICommand)element.GetValue(TapItProperty);
- }
- private static void TapChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var element = d as UIElement;
- if (element == null)
- {
- return;
- }
- if (e.NewValue != null)
- {
- element.Tapped += OnElementTap;
- }
- else
- {
- element.Tapped -= OnElementTap;
- }
- }
- private static void OnElementTap(object sender, TappedRoutedEventArgs e)
- {
- ICommand command = GetTapCommand(sender as DependencyObject);
- if (command != null)
- {
- if (command.CanExecute(e))
- {
- command.Execute(e);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment