There is a ListView in which elements of the name / time view should be displayed, with all elements being grouped by time.
With the conclusion of the elements there were no problems, but there were problems with the grouping. In particular, when navigating to a page, grouping headers are not displayed, but after selecting one of the elements, all headers become the same, namely, the same dates are displayed in all headers. When selecting another item, all headers change accordingly on the date of the selected item.
What could be the problem? I give the code using the MVVM template.
Model:
public class MapEvent { private string name; public string Name { get => name; set => name = value; } private DateTime start; public DateTime Start { get => start; set => start = value; } } ViewModel:
public class ScheduleViewModel { private CollectionViewSource sortedCollection; public CollectionViewSource SortedCollection { get => sortedCollection; set { sortedCollection = value; OnPropertyChanged(); } } public ScheduleViewModel() { using (MainDatabaseContext db = new MainDatabaseContext()) { MapEvents = new ObservableCollection<MapEvent>(db.MapEvents .OrderBy(i => i.Start) .ToList()); var groups = from i in MapEvents group i by i.Start.ToLocalTime().Date; SortedCollection.Source = groups; } } } View:
<Page.Resources> <ResourceDictionary> <CollectionViewSource x:Key="cvs" IsSourceGrouped="True" Source="{x:Bind ViewModel.SortedCollection.Source, Mode=OneWay}" /> <converters:FromNullableIntToIntConverter x:Key="fromNullableIntToIntConverter" /> </ResourceDictionary> </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ListView ItemsSource="{Binding Source={StaticResource cvs}, Mode=OneWay}" SelectedIndex="{x:Bind ViewModel.SelectedMapEvent, Mode=TwoWay, Converter={StaticResource fromNullableIntToIntConverter}}" <ListView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <TextBlock FontSize="30" FontWeight="SemiLight" Text="{Binding Source={StaticResource cvs}, Path=Start, Converter={StaticResource fromUtcToLocalTimeOnlyDateConverter}, Mode=OneWay}" /> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ListView.GroupStyle> </Grid> It is worth noting that when you go to the page, the following messages Error: BindingExpression path error: 'Start' property not found on 'Windows.UI.Xaml.Data.ICollectionView'. BindingExpression: Path='Start' DataItem='Windows.UI.Xaml.Data.ICollectionView'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String') in the debug console: Error: BindingExpression path error: 'Start' property not found on 'Windows.UI.Xaml.Data.ICollectionView'. BindingExpression: Path='Start' DataItem='Windows.UI.Xaml.Data.ICollectionView'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String') Error: BindingExpression path error: 'Start' property not found on 'Windows.UI.Xaml.Data.ICollectionView'. BindingExpression: Path='Start' DataItem='Windows.UI.Xaml.Data.ICollectionView'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')