Reputation: 1414
In my XAML I have:
<Label Text="{Binding DisplayName}" TextColor="Blue">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}"
CommandParameter="{Binding URL}" />
</Label.GestureRecognizers>
</Label>
In the view model (and I've checked the binding context) I have
private ICommand tapCommand;
public ICommand TapCommand
{
get { return tapCommand; }
}
public LinkReportPageViewModel()
{
PopulateLinkReport();
tapCommand = new Command( OnTapped );
}
public void OnTapped( object tappedURL )
{
Log.LogThis( $"Tapped on {tappedURL}", "LinkReportPageVM", DateTime.Now );
}
I expect that when I tap on the label I'll hit a breakpoint in OnTapped, but I don't. Am I missing something?
Note, this label is in a grouped list.
This was based on https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/
Thanks!!
Upvotes: 1
Views: 473
Reputation: 6953
As Diego mentioned, the binding context for the label is the model of the data item that the ListView is bound to. Try changing it to this:
<Label Text="{Binding DisplayName}" TextColor="Blue">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.TapCommand, Source={x:Reference NameOfListView}}"
CommandParameter="{Binding URL}" />
</Label.GestureRecognizers>
</Label>
Also ensure that you give your ListView an x:Name
We are telling the command that it's binding context is the parent ListView, which is bound to the ViewModel containing your command.
Upvotes: 2