vendredi 27 janvier 2017

Bind a checkbox in a listview to a class variable?

I would like to be able to get and set the state of a checkbox in a listview. I would like to either be able to automatically update MyListItems[row].myCheckedValue when the box is clicked by somehow binding in xaml (I know very little about binding) or to be able to loop through each list item by row and access the checkboxes in C#. I don't know how to approach either. I'm just starting out with WPF.

I Could also use Checked and Unchecked events, but I don't know how to retrieve the row of the list item the checkbox is in.

<ListView Name="listView">
   <ListView.ItemTemplate>
      <DataTemplate>
         <CheckBox x:Name="checkBox" Checked="itsChecked" Unchecked="itsUnchecked"/>
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>


public List<myListItem> MyListItems;
...
listView.ItemsSource = MyListItems;
...
public class myListItem {
   public bool myCheckedValue;
}


private void getCheckedItems() {
   //Loop through listview rows and get checkbox state
   //???
}


private void itsChecked(object sender, RoutedEventArgs e) {
   //How can I get the row this checkbox is in??
}




Aucun commentaire:

Enregistrer un commentaire