2

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

1
  • Focus on your data - SymptomList, not UI. With the boolean properties defined in symptom model class and binding TwoWay to your checkbox, could get the updated list from SymptomList then Commented Feb 15, 2023 at 20:36

1 Answer 1

0

Try using a command instead of the Clicked event, in order to handle your logic in the ViewModel instead.

<Button Text="Submit" Command="{Binding Submit}"/>

In your ViewModel, you handle your command:

public ViewModel()
{
    // ...
    Submit = new Command(ExecuteSubmit);
}

private ObservableCollection<Symptom> symptomList;
public ObservableCollection<Symptom> SymptomList
{
    get => symptomList;
    set
    {
        if (symptomList != value)
        {
            symptomList = value;
            OnPropertyChanged("SymptomList");
        }
    }
}
 
public void ExecuteSubmit()
{
    foreach(Symptom item in SymptomList)
    {
        if (item.IsChecked)
        {
            new AssessmentModel(UserID, item.Symptom, True, item.Severity);
        }
    }
    // Rest of your logic here
}
Sign up to request clarification or add additional context in comments.

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.