Reputation: 45
there are many similar issues on SO, but I haven't yet found a solution that works for me. I have a listview that does not appear at all when bound to a class member's List. I have many working bindings, I just can't figure out what I'm doing wrong with this one. My listview 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>
and my ViewModel's OnNavigatedTo, where the data is loaded into my member list:
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);
}
}
The method called that set's the dates in the List:
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");
}
and finally my binding in viewmodel:
private Timecard _displayTimecard;
public Timecard DisplayTimecard
{
get { return _displayTimecard; }
set
{
SetProperty(ref _displayTimecard, value);
}
}
I've searched through many existing questions, but I have not found a solution that works for me. If anyone could shed some light on this issue, or point me to an existing question, i would really appreciate it.
Upvotes: 0
Views: 155
Reputation: 45
OK - well I finally figured out what I was doing wrong, or at least what works now. From 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.
So, I changed my List to an observable Collection:
public ObservableCollection<DateTime> Dates
{
get { return _dates; }
set { SetProperty(ref _dates, value); }
}
And I changed my Xaml binding syntax as well:
<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>
I hope that my struggles might help someone, someday.
Upvotes: 2