dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CannotGroupView, view)

Error message

SR.Format(SR.CannotGroupView, view)

What it means

CollectionViewSource copies its GroupDescriptions onto the generated view only if the view supports grouping (CanGroup). If the view cannot group but GroupDescriptions contains entries, it throws InvalidOperationException with CannotGroupView.

Solutions

  1. Group the data at the source (e.g. GroupBy in LINQ with CollectionViewSource over grouped structures) or use a view that supports grouping
  2. Ensure the default ListCollectionView is generated (bind to IList-backed collections)
  3. Remove GroupDescriptions when the target view cannot group

Example fix

// before
cvs.GroupDescriptions.Add(new PropertyGroupDescription("Category")); // view.CanGroup == false
// after
cvs.Source = items.GroupBy(i => i.Category).Select(g => new { Category = g.Key, Items = g.ToList() }).ToList();
Defensive patterns

Strategy: fallback

Validate before calling

bool safe = !cvs.GroupDescriptions.Any() || cvs.View?.CanGroup == true;

Type guard

bool CanApplyGrouping(CollectionViewSource cvs) => cvs.View == null || cvs.View.CanGroup || !cvs.GroupDescriptions.Any();

Try / catch

try { ActivateView(); } catch (InvalidOperationException ex) when (ex.Message.Contains("CannotGroupView")) { GroupAtSourceInstead(); }

Prevention

When it happens

Trigger: Adding GroupDescriptions (e.g. PropertyGroupDescription) to a CollectionViewSource whose generated view reports CanGroup == false, such as a custom or minimal ICollectionView implementation.

Common situations: Grouping a bound collection whose view type doesn't implement ICollectionView grouping; overriding CollectionViewType to a custom non-grouping view while XAML defines group descriptions.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/e7bf8b84f5158203. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionViewSource.cs:1030

                if (view.CanFilter)
                {
                    view.Filter = filter;
                }
                else if (filter != null)
                    throw new InvalidOperationException(SR.Format(SR.CannotFilterView, view));

                // GroupBy
                if (view.CanGroup)
                {
                    view.GroupDescriptions.Clear();
                    for (i=0, n=GroupDescriptions.Count;  i < n;  ++i)
                    {
                        view.GroupDescriptions.Add(GroupDescriptions[i]);
                    }
                }
                else if (GroupDescriptions.Count > 0)
                    throw new InvalidOperationException(SR.Format(SR.CannotGroupView, view));

                // Live shaping
                if (liveView != null)
                {
                    ObservableCollection<string> properties;

                    // sorting
                    if (liveView.CanChangeLiveSorting)
                    {
                        liveView.IsLiveSorting = IsLiveSortingRequested;
                        properties = liveView.LiveSortingProperties;
                        properties.Clear();

                        if (IsLiveSortingRequested)
                        {
                            foreach (string s in LiveSortingProperties)
                            {
                                properties.Add(s);

View on GitHub (pinned to 81131a70a4)