3

I have a listview in Xamarin forms and I am trying to get a checkbox in each row with a command attached to it.

I have tried some things but nothing worked out yet, because I can't get the binded ID with it.

The things that I have already tried are:

  • Adding a CheckedChanged parameter to the checkbox, I can only get true or false, but not the item this is about.
<CheckBox VerticalOptions="End" x:Name="Checkbox" IsChecked="{Binding IsBought, Mode=TwoWay}" WidthRequest="100" CheckedChanged="Checkbox_CheckedChanged"/>
  • Tried with GestureRecognizers with CommandParameter, this won't even call the function attached to it. I tried this with Tapped and Command, maybe I need to do the command with Binding?
<CheckBox VerticalOptions="End" x:Name="Checkbox" IsChecked="{Binding IsBought, Mode=TwoWay}" WidthRequest="100" CheckedChanged="Checkbox_CheckedChanged">
  <CheckBox.GestureRecognizers>
    <TapGestureRecognizer Command="TapGestureRecognizer_Tapped" CommandParameter="{Binding ArtikelID}"/>
  </CheckBox.GestureRecognizers>
</CheckBox>

I hope you can help me further :) Have a nice day

2 Answers 2

6

use the sender's BindingContext

void OnCheckBoxCheckedChanged(object sender, CheckedChangedEventArgs e)
{
    var cb = (CheckBox)sender;
    var item = (MyModel)cb.BindingContext;
    var id = item.ArtikelID;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of MyModel I had to put the class that the list uses. Thanks!
0

As I understand you have a command property in your ViewModel, and you want to bind every checkBox's command to that command in ViewModel, and pass the item as parameter.

In your case you have to specify the DataContext for that binding, as it is pointing to the item in that list.

Use RelativeSource to solve your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.