{"record":{"id":"e5231be03cf3b6eb","repo":"dotnet/wpf","slug":"sr-unexpectedcollectionchangeaction-treeviewitem","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/TreeViewItem.cs","lineNumber":979,"sourceCode":"                        if (tree != null)\n                        {\n                            // When Selected item is replaced - 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 = tree.SelectedItem;\n                            if ((selectedItem != null) && selectedItem.Equals(e.OldItems[0]))\n                            {\n                                tree.ChangeSelection(selectedItem, tree.SelectedContainer, false);\n                            }\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        /// <summary>\n        /// Recursively & syncronously expand all the nodes in this subtree.\n        /// </summary>\n        private static void ExpandRecursive(TreeViewItem item)\n        {\n            if (item == null)\n            {\n                return;\n            }\n\n            // Expand the current item\n            if (!item.IsExpanded)\n            {\n                item.SetCurrentValueInternal(IsExpandedProperty, BooleanBoxes.TrueBox);\n            }","sourceCodeStart":961,"sourceCodeEnd":997,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TreeViewItem.cs#L961-L997","documentation":"TreeViewItem's own OnItemsChanged, like TreeView's, throws NotSupportedException for any NotifyCollectionChangedAction it does not explicitly handle (only Add and Move pass through unhandled in the shown default switch). It indicates the child-items source raised a change action the node cannot process.","triggerScenarios":"A TreeViewItem's ItemsSource collection raising an unexpected/invalid collection-change action, or a custom child collection emitting non-standard actions.","commonSituations":"Custom hierarchical collections with hand-written change notification; data layer emitting unusual actions on nested nodes; third-party observable tree collections.","solutions":["Back child items with ObservableCollection<T>.","Correct the custom collection's RaiseChanged calls to use valid actions.","Raise a Reset notification instead of unsupported fine-grained actions."],"exampleFix":"// before\nhandler(this, new NotifyCollectionChangedEventArgs((NotifyCollectionChangedAction)99));\n// after\nhandler(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));","handlingStrategy":"validation","validationCode":"if (e.Action is NotifyCollectionChangedAction.Add or NotifyCollectionChangedAction.Move) { /* handled */ }\nelse { /* rebuild or Reset instead */ }","typeGuard":null,"tryCatchPattern":"try { node.Items.Add(item); }\ncatch (NotSupportedException) { /* child collection raised unsupported action; reset node.ItemsSource */ }","preventionTips":["Back TreeViewItem items with ObservableCollection<T>.","Validate custom hierarchical collection change notifications.","Prefer Reset notifications for unusual bulk operations."],"tags":["wpf","not-supported","treeviewitem","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"}