Description
On Windows, object of the class CollectionView throws NullReferenceException when items' model is changed from grouped list to flat list and value of the IsGrouped property is set to false.
Below is how CollectionView is configured is XAML:
<CollectionView Grid.Row="2" ItemsSource="{Binding Items}" IsGrouped="{Binding IsGrouped}">
<CollectionView.GroupHeaderTemplate>
<DataTemplate>
<Label Margin="2" FontSize="Medium" Text="{Binding ItemText}" HorizontalOptions="Fill" TextColor="LightBlue"/>
</DataTemplate>
</CollectionView.GroupHeaderTemplate>
<CollectionView.ItemTemplate>
<DataTemplate>
<Label Margin="15,2" Text="{Binding ItemText}" HorizontalOptions="Fill" TextColor="Gray"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Object of the class ItemListViewModel is set to BindingContext. When the checkbox (located above the CollectionView) is clicked a new value is set to the IsGrouped property from class ItemListViewModel and the items' list is recreated:
private void Populate()
{
try
{
ErrorMessage = String.Empty;
items.Clear();
if (isGrouped)
{
int groupIndex = 0;
items.Add(GetGroup(groupIndex++, 2));
items.Add(GetGroup(groupIndex++, 1));
items.Add(GetGroup(groupIndex++, 3));
items.Add(GetGroup(groupIndex++, 2));
}
else
{
int itemIndex = 0;
items.Add(new ItemViewModel(itemIndex++));
items.Add(new ItemViewModel(itemIndex++));
items.Add(new ItemViewModel(itemIndex++));
}
}
catch (Exception ex)
{
ErrorMessage = $"Model creation error: {ex}";
}
}
public bool IsGrouped
{
get => isGrouped;
set
{
isGrouped = value;
Populate();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsGrouped)));
}
}
When the IsGrouped property value is changed from false to true, no exception is thrown and the CollectionView is correctly updated.
The problem does not exist on Android.
The main page just after the startup:

The main page after the checkbox was unchecked:

Steps to Reproduce
1. Download the code of the linked project and start the app.
2. Uncheck the check box with label "Is Grouping Active".
Link to public reproduction project repository
https://github.com/jns300/Bugs/tree/main/CollectionViewGroupingIssue
Version with bug
7.0.92
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
Windows 11
Did you find any workaround?
No response
Relevant log output
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Maui.Controls.Platform.ItemTemplateContextEnumerable.GetEnumerator()+MoveNext()
at ABI.System.Collections.IEnumerable.ToAbiHelper.NonGenericToGenericEnumerator.MoveNext()
at ABI.System.Collections.Generic.IEnumerator`1.ToAbiHelper._MoveNext()
at ABI.System.Collections.Generic.IEnumerator`1.ToAbiHelper.get_HasCurrent()
at ABI.Microsoft.UI.Xaml.Interop.IBindableIterator.Vftbl.Do_Abi_get_HasCurrent_1(IntPtr thisPtr, Byte* value)
--- End of stack trace from previous location ---
at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
at ABI.System.Collections.Specialized.NotifyCollectionChangedEventHandler.NativeDelegateWrapper.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at Microsoft.Maui.Controls.Platform.GroupedItemTemplateCollection.Add(NotifyCollectionChangedEventArgs args)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at CollectionViewGroupingIssue.ViewModel.ItemListViewModel.Populate()
Description
On Windows, object of the class
CollectionViewthrowsNullReferenceExceptionwhen items' model is changed from grouped list to flat list and value of theIsGroupedproperty is set tofalse.Below is how
CollectionViewis configured is XAML:Object of the class
ItemListViewModelis set toBindingContext. When the checkbox (located above theCollectionView) is clicked a new value is set to theIsGroupedproperty from classItemListViewModeland the items' list is recreated:When the
IsGroupedproperty value is changed fromfalsetotrue, no exception is thrown and theCollectionViewis correctly updated.The problem does not exist on Android.
The main page just after the startup:
The main page after the checkbox was unchecked:
Steps to Reproduce
Link to public reproduction project repository
https://github.com/jns300/Bugs/tree/main/CollectionViewGroupingIssue
Version with bug
7.0.92
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
Windows 11
Did you find any workaround?
No response
Relevant log output