{"record":{"id":"a0a5c6e33c4da947","repo":"dotnet/wpf","slug":"sr-format-sr-cannotsortview-view","errorCode":null,"errorMessage":"SR.Format(SR.CannotSortView, view)","messagePattern":"SR\\.Format\\(SR\\.CannotSortView, view\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionViewSource.cs","lineNumber":1000,"sourceCode":"                int i, n;\n\n                // Culture\n                if (Culture != null)\n                {\n                    view.Culture = Culture;\n                }\n\n                // Sort\n                if (view.CanSort)\n                {\n                    view.SortDescriptions.Clear();\n                    for (i=0, n=SortDescriptions.Count;  i < n;  ++i)\n                    {\n                        view.SortDescriptions.Add(SortDescriptions[i]);\n                    }\n                }\n                else if (SortDescriptions.Count > 0)\n                    throw new InvalidOperationException(SR.Format(SR.CannotSortView, view));\n\n                // Filter\n                Predicate<object> filter;\n                if (FilterHandlersField.GetValue(this) != null)\n                {\n                    filter = FilterWrapper;\n                }\n                else\n                {\n                    filter = null;\n                }\n\n                if (view.CanFilter)\n                {\n                    view.Filter = filter;\n                }\n                else if (filter != null)\n                    throw new InvalidOperationException(SR.Format(SR.CannotFilterView, view));","sourceCodeStart":982,"sourceCodeEnd":1018,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionViewSource.cs#L982-L1018","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ncvs.SortDescriptions.Add(new SortDescription(\"Name\", ListSortDirection.Ascending)); // view.CanSort == false\n// after\nvar sorted = items.OrderBy(i => i.Name).ToList();\ncvs.Source = sorted; // sort at the source","handlingStrategy":"fallback","validationCode":"var view = (ICollectionView)typeof(CollectionViewSource).GetMethod(\"GetDefaultView\", BindingFlags.NonPublic|BindingFlags.Instance)?.Invoke(cvs, null) ?? cvs.View; bool safe = !cvs.SortDescriptions.Any() || view?.CanSort == true;","typeGuard":"bool CanApplySorting(CollectionViewSource cvs) => cvs.View == null || cvs.View.CanSort || !cvs.SortDescriptions.Any();","tryCatchPattern":"try { ActivateView(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"CannotSortView\")) { SortAtSourceInstead(); }","preventionTips":["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"],"tags":["wpf","data-binding","sorting","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}