I'm using a custom ViewCell defined in C# to populate a ListView in Xamarin.Forms. The ViewCell contains an Image, that acts as a button and when clicked, should be able to manipulate the underlying data of the ListView.
Basically I am looking for the code behind equivalent for:
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Path=BindingContext.PlusClickedCommand, Source={x:Reference Name=ChallengesLV}}"
CommandParameter="{Binding}"/>
</Image.GestureRecognizers>
I have tried:
plusButton.GestureRecognizers.Add(new TapGestureRecognizer() {
Command = bindingContext.AddProgressToChallengeCommand,
CommandParameter = new Binding(".")} });
But it gives me an NullReference exception when I try to access the object of type Challenge in my viewmodel like so:
AddProgressToChallengeCommand = new Command( (sender) => doSomething((Challenge)sender) );
When I inspect the sender object in debug mode, it tells me it is an object of type Binding with every property null except path="."
How do I get the Item this Binding refers to?
Thanks to Jason: The Command needs to be initialized like that, to access the parameter:
AddProgressToChallengeCommand = new Command<Challenge>( (challenge) =>
doSomething(challenge);
And for the recognizer binding, I needed to change it to:
var recognizer = new TapGestureRecognizer() {Command = bindingContext.AddProgressToChallengeCommand};
recognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
plusButton.GestureRegognizers.Add(recognizer);