【发布时间】:2023-02-15 18:47:54
【问题描述】:
我有一个 ListView,它绑定到视图模型中的一些数据。在 ListView 中,每一行都是一个复选框和症状名称(例如咳嗽)。在 ListView 的末尾,在页脚中,我有一个按钮用于提交用户已选中的所有复选框。
<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>
在后面的代码中,我希望按下按钮将选中的复选框作为每个选中项目的新对象提交。注释代码中解释了格式 -
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();
}
我将如何执行此操作?据我所见,无法循环遍历 ListView 项目以查找已检查的项目。任何帮助表示赞赏
【问题讨论】:
标签: c# xamarin xamarin.forms checkbox android-listview