xamarin - 我有一个收藏视图中的选择器,但我不确定如何获取价值
问题描述
我有一个collectionview,它有一个[标签(星期几)-[复选框]-[带时间的选择器]。
我希望用户能够选择日期和时间,然后我想将这些值传递给我的数据库。到目前为止,我能够选择日期并传递这个值。但是,我正在努力引用选择器中的值。我认为这是因为它是我对象中的一个列表。我试过 booking.Listtimes 但这只是一个列表。我想要选择的值。
我的视图模型:
public class RepeatMonthly
{
public string Day { get; set; }
public bool Selected { get; set; }
public List<WalkTimes> _ListTimes { get; set; }
}
private WalkTimes _selectedTimes;
public WalkTimes SelectedTimes
{
get
{
return _selectedTimes;
}
set
{
SetProperty(ref _selectedTimes, value);
}
}
public ObservableCollection<RepeatMonthly> DayList { get; set; }
public CreateWeeklyScheduleViewModel()
{
ListTimes = WalkTimesService.GetTimes().OrderBy(c => c.Key).ToList();
DayList = new ObservableCollection<RepeatMonthly>()
{
new RepeatMonthly(){Day="Every Monday", Selected = false, _ListTimes = ListTimes},
new RepeatMonthly(){Day="Every Tuesday", Selected = false, _ListTimes = ListTimes},
new RepeatMonthly(){Day="Every WednesDay", Selected = false, _ListTimes = ListTimes},
new RepeatMonthly(){Day="Every Thursday", Selected = false, _ListTimes = ListTimes},
new RepeatMonthly(){Day="Every Friday", Selected = false, _ListTimes = ListTimes},
new RepeatMonthly(){Day="Every Saturday", Selected = false, _ListTimes = ListTimes},
new RepeatMonthly(){Day="Every Sunday", Selected = false, _ListTimes = ListTimes}
};
source = new ObservableCollection<PetProfile>();
}
我的 Xaml:
<CollectionView x:Name="RepeatCollectionView" HorizontalOptions="Center" ItemsSource="{Binding DayList}" HeightRequest="350">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="0" RowDefinitions="25, 20" ColumnDefinitions="*,*">
<Label Text="{Binding Day}" FontAttributes="Bold" FontSize="Medium"
Margin="5,0"
VerticalTextAlignment="Center" HorizontalTextAlignment="Start"/>
<CheckBox x:Name="SelectDayCheckBox" Grid.Row="0" HorizontalOptions="End" IsChecked="{Binding Selected, Mode=TwoWay}" BindingContext="{Binding .}" CheckedChanged="SelectDayCheckBox_CheckedChanged"/>
<Picker x:Name="SelectTimeOfWalkPicker" Title="--Select Walk Start Time--" Grid.Column="1" ItemsSource="{Binding _ListTimes}" ItemDisplayBinding="{Binding Value}" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" SelectedItem="{Binding SelectedTimes}" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
我的 .cs 页面:
private async void SubmitBtn_Clicked(object sender, EventArgs e)
{
foreach (RepeatMonthly booking in (BindingContext as CreateWeeklyScheduleViewModel).DayList)
{
if (booking.Selected)
{
await passtoDataBase(booking.Day, "where time variable goes");
}
}
await DisplayAlert("Success", "Booked Successfully", "OK");
await Shell.Current.GoToAsync($"//{nameof(MyBookingsPage)}");
}
WalkTimes 对象的属性:
public class WalkTimes
{
public int Key { get; set; }
public string Value { get; set; }
}
解决方案
推荐阅读
- javascript - 执行“SELECT COUNT”查询后,无法从 RowDataPacket 访问“COUNT(id)”
- c# - 如果从子实例调用,则不会调用具有正确数据类型的父方法
- scala - 如何实现生成自然数 1、3、5、7...的函数?
- spring - Spring MVC 中的安全控制器访问
- redis - 为什么排序集中不支持差异运算符?
- python - 更新文件中的值并忽略python中的变量是否没有输入
- java - 将 Json 有效负载转换为 java 对象
- java - 如何在不同于“应用程序”和“活动”的扩展中实现全局变量?
- html - 根据父元素(SASS)的宽度在SASS中设置子元素的背景颜色
- python - numpy 2D @ 1D matmul 结果中的问题