I have a label which I am using to navigate to another page in my app via a tap. This works fine using the Tapped with a TapGestureRecognizer but when trying to implement MVVM and move the logic out of the code-behind, I had some trouble getting the Tapped event to fire a command on my view model.
I can solve this with the following implementation:
<Label Text="{Binding Name}">
<Label.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Source={RelativeSource
AncestorType={x:Type viewModels:HabitsOverviewViewModel}},
Path=TapCommand}"
CommandParameter="{Binding .}" />
</Label.GestureRecognizers>
</Label>
However for other events, i.e. Appearing, I have successfully used EventToCommandBehaviour from the MVVM tookit.
For consistency and readability, I wanted to also use EventToCommandBehaviour here, but cannot seem to get it to work. The TapGestureRecognizer has no Behaviours property, and if I define it under Label.Behaviours, Tapped is not recognised as a valid event.
Is this functionality available, or is my code just wrong?
This is what I have tried with the EventToCommandBehavior in a couple of different places but tapping does nothing (TappedCommand exists in the view model and is successfully trigged in the above implementation):
<Label Text="{Binding Name}">
<Label.GestureRecognizers>
<toolkit:EventToCommandBehavior
Command="{Binding TapCommand}"
CommandParameter="{Binding .}"
EventName="Tapped"
x:DataType="viewModels:MyViewModel" /> <!-- x:DataType is defined differently in the outer scope as this sits within a collection view -->
</Label.GestureRecognizers>
</Label>
See the following article: Label
A label has no "Tap" or "Tapped" event, therefore you cannot work with EventToCommandBehavior in that way. The right way to use a Tap command is how you did it in the first place.