Skip to content

[Windows] CollectionView throws NRE when value of IsGrouped property is changed to false #17864

Description

@jns300

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:

image

The main page after the checkbox was unchecked:

image

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()

Activity

  1. added
    s/verifiedVerified / Reproducible Issue ready for Engineering Triage
    s/triagedIssue has been reviewed
    on Oct 10, 2023
  2. XamlTest commented on Oct 10, 2023

    @XamlTest

    Verified this on Visual Studio Enterprise 17.8.0 Preview 2.0(8.0.0-rc.1.9171). Repro on Windows 11, not repro on Android 13.0-API33.
    Repo: CollectionViewGroupingIssue.zip

    iOS: Different error than Windows platform.
    Grouping

  3. added this to the Backlog milestone on Jun 5, 2024
  4. added a commit that references this issue on Mar 24, 2026
    ba42322
  5. added a commit that references this issue on Mar 24, 2026
    b89847a
  6. added a commit that references this issue on Apr 4, 2026
    355bf6c
  7. added a commit that references this issue on Apr 8, 2026
  8. modified the milestones: Backlog, .NET 10 SR6 on Apr 29, 2026
  9. locked and limited conversation to collaborators on May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-controls-collectionviewCollectionView, CarouselView, IndicatorViewhighIt doesn't work at all, crashes or has a big impact.partner/syncfusionIssues / PR's with Syncfusion collaborationplatform/windowss/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions