Genom
Topic Author
Posts: 47
Joined: 04 Dec 2014, 14:47

Touch command in a (whole) grid

22 Sep 2015, 21:11

Hi guys, any idea how to bind a command from a grid in xaml like a button?

(obviusly I don't want to put the grid within a button)

thanks!
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Touch command in a (wole) grid

25 Sep 2015, 10:29

Hi,

I'm sorry but I don't understand what you are trying to accomplish :)
Could you please elaborate a bit more the situation you want to solve?

Thanks.
 
Genom
Topic Author
Posts: 47
Joined: 04 Dec 2014, 14:47

Re: Touch command in a (wole) grid

26 Sep 2015, 09:48

Hi, basically something like <Grid Command="MyCommand" ....">


I don't know if possible but also it would be great something like the next as well:


<Grid TouchDown="MyTouchDownCommand" ....">
<Grid TouchMove="MyTouchMoveCommand" ....">
<Grid DoubleTouch="MyDoubleTouchCommand" ....">

an so on with different touch events and controls (images, textblocls, etc..)

cheers!
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Touch command in a (wole) grid

28 Sep 2015, 15:44

You can use attached properties to add this kind of behaviors.
public class TouchBehaviors: DependencyObject
{
  public static DependencyProperty TouchDownProperty = DependencyProperty.Register(
    "TouchDown", typeof(ICommand), typeof(TouchBehaviors),
    new PropertyMetadata(null, new PropertyChangedCallback(OnTouchDownChanged)));

  public static ICommand GetTouchDown(DependencyObject d)
  {
     return (ICommand)d.GetValue(TouchDownProperty);
  }

  public static void SetTouchDown(DependencyObject d, ICommand command)
  {
    d.SetValue(TouchDownProperty, command);
  }

  private static void OnTouchDownChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    UIElement element = d as UIElement;
    if (element != null)
    {
      element.TouchDown += OnTouchDown;
    }
  }

  private static void OnTouchDown(object sender, TouchEventArgs e)
  {
    DependencyObject d = sender as DependencyObject;
    if (d != null)
    {
      ICommand command = GetTouchDown(d);
      if (command != null)
      {
        if (command.CanExecute(null))
        {
          command.Execute(null);
        }
      }
    }
  }
} 
And then you can set this property in your xaml:
<Grid local:TouchBehaviors.TouchDown="{Binding TouchDownCommand}"/>
 
Genom
Topic Author
Posts: 47
Joined: 04 Dec 2014, 14:47

Re: Touch command in a (wole) grid

01 Oct 2015, 17:00

Awesome thank you so much!

My suggestion would be to add a chapter with this subject to the documentation, as it could probably help others.

cheers!

Who is online

Users browsing this forum: No registered users and 1 guest