I have a ListView which is bound to some data I have in the viewmodel. In the ListView, each row is a check box and the name of a symptom (e.g. coughing). At the end of the ListView, in the footer, I have a button to submit all of the checkboxes the user has checked.
<ListView x:Name="SymptomsListView" ItemsSource="{Binding SymptomList}" HasUnevenRows="True" SelectionMode="None" Footer="">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="12,0,0,0">
<CheckBox IsChecked="{Binding IsChecked}" Color="ForestGreen" WidthRequest="50" HeightRequest="50" />
<Label Text="{Binding SymptomName}" VerticalOptions="Center" Margin="0,20" FontSize="24" TextColor="Black" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.FooterTemplate>
<DataTemplate>
<ContentView>
<StackLayout>
<Button Text="Submit" TextColor="{StaticResource AppTextColor}" FontAttributes="Bold" FontSize="20" WidthRequest="220" HeightRequest="50" Margin="0,15" HorizontalOptions="Center" CornerRadius="10" BackgroundColor="{StaticResource ButtonColor}" Clicked="SubmitClick"/>
</StackLayout>
</ContentView>
</DataTemplate>
</ListView.FooterTemplate>
</ListView>
In the code behind, I want the button press to submit the checked check boxes as a new object per checked item. Format is explained in the commented code -
protected void SubmitClick(object sender, EventArgs e)
{
// Result output per symptom for the user e.g.
// If Breathlessness is checked with severity 4, Cough is checked with severity 8 & PTSD is checked with severity 2
// new AssessmentModel(UserID, Breathlessness, True, 4);
// new AssessmentModel(UserID, Cough, True, 8);
// new AssessmentModel(UserID, PTSD, True, 2);
// CODE HERE
this.Navigation.PopAsync();
}
How would I perform this? From what I can see there is no way to loop over ListView items to find which have been checked. Any help appreciated
SymptomList, not UI. With the boolean properties defined in symptom model class and binding TwoWay to your checkbox, could get the updated list fromSymptomListthen