Alan2
Alan2

Reputation: 24572

How can I link a Grid gesture to a View Model command?

Here's what I tried so far:

<Grid x:Name="wordGrid" Padding="10,0,10,0">
   <Grid.GestureRecognizers>
      <TapGestureRecognizer Command="{Binding wordGridClickedCommand}" />
   </Grid.GestureRecognizers>

In the view model:

public class PhrasesFrameViewModel : ObservableProperty
{

    public PhrasesFrameViewModel()
    {
        var wordGridClickedCommand = new Command(() =>
        {
            if (App.Timer1Running)
                ResetTimer1();
            else
                ResetTimer2();
        });
    }

When I put a breakpoint in the command it doesn't get reached when I tap on the grid.

What might I be doing wrong?

Upvotes: 1

Views: 53

Answers (1)

Sharada
Sharada

Reputation: 13601

Binding only works with properties.

You can expose a property in your view model as:

private ICommand wordGridClickedCommand;
pubilc ICommand WordGridClickedCommand 
{ 
    get { return wordGridClickedCommand; } 
    set 
    { 
        wordGridClickedCommand = value;
        OnPropertyChanged();
    }
}

and make sure to update binding path to WordGridClickedCommand in XAML.

EDIT-1

Or you can use:

private ICommand wordGridClickedCommand;
pubilc ICommand WordGridClickedCommand 
{ 
    get 
    { 
        return wordGridClickedCommand ?? 
            (wordGridClickedCommand = new Command(() =>
            {
                if (App.Timer1Running)
                    ResetTimer1();
                else
                    ResetTimer2();
            }));
    }
}

Upvotes: 2

Related Questions