首页 > 解决方案 > 我有一个收藏视图中的选择器,但我不确定如何获取价值

问题描述

我有一个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; }
}

标签: xamarinxamarin.formsuicollectionviewpicker

解决方案


推荐阅读