dotnet/wpf · error · InvalidOperationException
SR.Format(SR.CannotSortView, view)
Error message
SR.Format(SR.CannotSortView, view)
What it means
CollectionViewSource copies its SortDescriptions onto the generated view only if the view supports sorting (CanSort). If the underlying view's CanSort is false but the CollectionViewSource has one or more sort descriptions, it throws InvalidOperationException with CannotSortView because the sort request cannot be honored.
Solutions
- Sort the underlying collection itself before binding when the view cannot sort
- Ensure the generated view supports sorting (default ListCollectionView over IList does)
- Sort the source data (e.g. OrderBy in LINQ) and reassign Source instead of using SortDescriptions
Example fix
// before
cvs.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); // view.CanSort == false
// after
var sorted = items.OrderBy(i => i.Name).ToList();
cvs.Source = sorted; // sort at the source Defensive patterns
Strategy: fallback
Validate before calling
var view = (ICollectionView)typeof(CollectionViewSource).GetMethod("GetDefaultView", BindingFlags.NonPublic|BindingFlags.Instance)?.Invoke(cvs, null) ?? cvs.View; bool safe = !cvs.SortDescriptions.Any() || view?.CanSort == true; Type guard
bool CanApplySorting(CollectionViewSource cvs) => cvs.View == null || cvs.View.CanSort || !cvs.SortDescriptions.Any();
Try / catch
try { ActivateView(); } catch (InvalidOperationException ex) when (ex.Message.Contains("CannotSortView")) { SortAtSourceInstead(); } Prevention
- Check view.CanSort before adding SortDescriptions
- Prefer sorting the source collection over the view for non-standard views
- Avoid overriding CollectionViewType with views lacking sort support
When it happens
Trigger: Binding CollectionViewSource.Source to a collection whose default view does not implement sorting (e.g. some custom ICollectionView implementations) while SortDescriptions contains entries; assigning SortDescriptions before/without a sortable view type.
Common situations: Using CollectionViewSource over a custom collection with a non-sorting view; ObservableCollection views normally support sorting via ListCollectionView, but custom view factories (CollectionViewType override) may produce non-sorting views.
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
- SR.CannotDetermineSortByPropertiesForCollection
- SR.Format(SR.CannotFilterView, view)
- SR.Format(SR.CannotGroupView, view)
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "CustomSort")
- SR.Format(SR.MemberNotAllowedDuringAddOrEdit, "Sorting")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a0a5c6e33c4da947.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionViewSource.cs:1000
int i, n;
// Culture
if (Culture != null)
{
view.Culture = Culture;
}
// Sort
if (view.CanSort)
{
view.SortDescriptions.Clear();
for (i=0, n=SortDescriptions.Count; i < n; ++i)
{
view.SortDescriptions.Add(SortDescriptions[i]);
}
}
else if (SortDescriptions.Count > 0)
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));View on GitHub (pinned to 81131a70a4)