{"record":{"id":"0d96d5bfa05cc9e2","repo":"dotnet/wpf","slug":"sr-unexpectedcollectionchangeaction-treeview","errorCode":null,"errorMessage":"SR.UnexpectedCollectionChangeAction","messagePattern":"SR\\.UnexpectedCollectionChangeAction","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TreeView.cs","lineNumber":439,"sourceCode":"\n                case NotifyCollectionChangedAction.Replace:\n                    {\n                        // If old item is selected - remove the selection\n                        // Revisit the condition when we support duplicate items in Items collection: if e.OldItems[0] is the same as selected items we will unselect the selected item\n                        object selectedItem = SelectedItem;\n                        if ((selectedItem != null) && selectedItem.Equals(e.OldItems[0]))\n                        {\n                            ChangeSelection(selectedItem, _selectedContainer, false);\n                        }\n                    }\n                    break;\n\n                case NotifyCollectionChangedAction.Add:\n                case NotifyCollectionChangedAction.Move:\n                    break;\n\n                default:\n                    throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, e.Action));\n            }\n        }\n\n        private void SelectFirstItem()\n        {\n            object item;\n            TreeViewItem container;\n            bool selected = GetFirstItem(out item, out container);\n            if (!selected)\n            {\n                item = SelectedItem;\n                container = _selectedContainer;\n            }\n\n            ChangeSelection(item, container, selected);\n        }\n\n        private bool GetFirstItem(out object item, out TreeViewItem container)","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TreeView.cs#L421-L457","documentation":"TreeView.OnItemsChanged only supports Add, Move, Remove, Replace and Reset notifications it handles explicitly; any other NotifyCollectionChangedAction reaching the default case throws NotSupportedException with SR.UnexpectedCollectionChangeAction. It signals that the source collection raised a change action the TreeView cannot process.","triggerScenarios":"Binding TreeView.ItemsSource to a collection whose INotifyCollectionChanged implementation raises an unexpected/invalid action value, or a custom ObservableCollection raising non-standard actions.","commonSituations":"Custom collection classes with hand-rolled CollectionChanged events; third-party bindable collections that misreport actions; framework version mismatches where a new action value appears.","solutions":["Use ObservableCollection<T> or a standard INotifyCollectionChanged implementation for ItemsSource.","Fix the custom collection to only raise valid NotifyCollectionChangedAction values.","Replace the whole ItemsSource instead of emitting exotic change actions from the source collection."],"exampleFix":"// before\ncustomCollection.RaiseCollectionChanged(NotifyCollectionChangedAction.Drag); // bogus action\n// after\nusing (customCollection.SuppressNotifications())\n    customCollection.RefreshItems(); // or raise Reset","handlingStrategy":"validation","validationCode":"if (e.Action is NotifyCollectionChangedAction.Add or NotifyCollectionChangedAction.Move\n    or NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Replace\n    or NotifyCollectionChangedAction.Reset) { /* acceptable source */ }","typeGuard":null,"tryCatchPattern":"try { treeView.ItemsSource = myCollection; }\ncatch (NotSupportedException ex) { /* collection raises unsupported actions; swap to ObservableCollection */ }","preventionTips":["Use ObservableCollection<T> as ItemsSource.","Audit custom INotifyCollectionChanged implementations for valid action values.","Raise Reset for bulk changes instead of exotic actions."],"tags":["wpf","not-supported","treeview","collection-changed"],"backgroundTag":"unsupported-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}