【问题标题】:Xamarin ListView not appearing with DateTime BindingXamarin ListView 未与 DateTime 绑定一起出现
【发布时间】:2020-06-10 23:07:55
【问题描述】:

在 SO 上有很多类似的问题,但我还没有找到适合我的解决方案。我有一个列表视图,当绑定到班级成员的列表时根本不会出现。我有很多有效的绑定,我只是不知道我做错了什么。我的列表视图 XAML:

<ListView ItemsSource="{Binding DisplayTimecard.Days}"
                  SelectedItem="{Binding SelectedItem}">
            <ListView.Behaviors>
            <ioc:EventToCommandBehavior EventName="ItemTapped" 
                                      Command="{Binding ClickedDateCommand}"
                                      EventArgsConverter="{StaticResource ItemTappedEventArgsConverter}" />
            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical">
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding Path=Date, StringFormat=dd-MM-yyyy}"/>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

还有我的 ViewModel 的 OnNavigatedTo,数据被加载到我的成员列表中:

 public async override void OnNavigatedTo(INavigationParameters parameters)
        {
            PayPeriodsList = PayPeriods;
            RaisePropertyChanged("PayPeriodsList");
            SelectedPayPeriod = parameters["payperiod"] as PayPeriod;
            RaisePropertyChanged("SelectedPayPeriod");
            DisplayTimecard = SelectedPayPeriod.Timecards.First(x => x.EmployeesID == SelectedEmployee.EmployeeID);
            RaisePropertyChanged("DisplayTimecard");
            SetDisplayDates();

            if (SelectedEmployee != null)
            {
                UserName = "User: " + SelectedEmployee.EmployeeUsername;
                RaisePropertyChanged(UserName);
            }
        }

调用该集合的方法是列表中的日期:

 public void SetDisplayDates()
        {
            for (var date = SelectedPayPeriod.StartDate; date <= SelectedPayPeriod.EndDate; date = date.AddDays(1))
            {
                DisplayTimecard.Days.Add(date);//List<DateTime> named in ListView's ItemsSource
            }
            RaisePropertyChanged("DisplayTimecard.Days");
        }

最后是我在视图模型中的绑定:

private Timecard _displayTimecard;
        public Timecard DisplayTimecard
        {
            get { return _displayTimecard; }
            set
            {
                SetProperty(ref _displayTimecard, value);
            }
        }

我搜索了许多现有问题,但没有找到适合我的解决方案。如果有人能对这个问题有所了解,或者指出一个现有的问题,我将不胜感激。

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    好的 - 好吧,我终于弄清楚了我做错了什么,或者至少现在是什么。来自 MS Docs:

    If you want the ListView to automatically update as items are added, removed and changed in the underlying list, you'll need to use an ObservableCollection.
    

    所以,我将我的列表更改为可观察的集合:

     public ObservableCollection<DateTime> Dates
            {
                get { return _dates; }
                set { SetProperty(ref _dates, value); }
            }
    

    我还更改了我的 Xaml 绑定语法:

    <ListView ItemsSource="{Binding Dates}"
                      SelectedItem="{Binding SelectedItem}">
                <ListView.Behaviors>
                <ioc:EventToCommandBehavior EventName="ItemTapped" 
                                          Command="{Binding ClickedDateCommand}"
                                          EventArgsConverter="{StaticResource ItemTappedEventArgsConverter}" />
                </ListView.Behaviors>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Vertical">
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="{Binding Date, StringFormat='{0:dd-MM-yyyy}'}"/>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    我希望有一天我的努力能对某人有所帮助。

    【讨论】:

    • 您可以标记它,这将帮助更多有相同问题的人:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多