dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CannotFilterView, view)

Error message

SR.Format(SR.CannotFilterView, view)

What it means

CollectionViewSource applies its Filter to the generated view only when the view supports filtering (CanFilter). If the view cannot filter but a filter (via FilterHandlersField, i.e. the Filter event/property) is set, it throws InvalidOperationException with CannotFilterView.

Solutions

  1. Filter the underlying collection before assigning Source
  2. Make sure the view supports filtering (default views over IList do via ListCollectionView)
  3. Move the predicate into the source data pipeline (e.g. Where in LINQ) instead of the view

Example fix

// before
cvs.Filter += (s, e) => e.Accepted = ((Item)e.Item).IsActive; // view.CanFilter == false
// after
cvs.Source = items.Where(i => i.IsActive).ToList();
Defensive patterns

Strategy: fallback

Validate before calling

bool safe = !hasFilterHandler(cvs) || cvs.View?.CanFilter != false; // attach filter only when view can filter

Type guard

bool CanApplyFilter(CollectionViewSource cvs) => cvs.View == null || cvs.View.CanFilter;

Try / catch

try { ActivateView(); } catch (InvalidOperationException ex) when (ex.Message.Contains("CannotFilterView")) { FilterAtSourceInstead(); }

Prevention

When it happens

Trigger: Setting cvs.Filter += handler or the Filter property while the resolved view's CanFilter is false, e.g. a custom ICollectionView that doesn't implement filtering over the source collection.

Common situations: Custom collection views that skip filtering support; source collections whose default view is not a ListCollectionView; view type overridden via CollectionViewType to a minimal custom implementation.

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/19b6ad5a6eebe555. Report an issue: GitHub.

Appendix: source

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

                    throw new InvalidOperationException(SR.Format(SR.CannotSortView, view));

                // Filter
                Predicate<object> filter;
                if (FilterHandlersField.GetValue(this) != null)
                {
                    filter = FilterWrapper;
                }
                else
                {
                    filter = null;
                }

                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;

View on GitHub (pinned to 81131a70a4)