{"record":{"id":"a9f81e5d76d7f34e","repo":"dotnet/wpf","slug":"sr-format-sr-unexpectedcollectionchangeaction-e-action","errorCode":null,"errorMessage":"SR.Format(SR.UnexpectedCollectionChangeAction, e.Action)","messagePattern":"SR\\.Format\\(SR\\.UnexpectedCollectionChangeAction, e\\.Action\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs","lineNumber":2229,"sourceCode":"                        {\n                            clone.RemoveAt(e.OldStartingIndex);\n                        }\n                        for (int i = 0; i < e.NewItems.Count; i++)\n                        {\n                            clone.Insert(e.NewStartingIndex + i, (T)e.NewItems[i]);\n                        }\n                    }\n                    break;\n\n                // this arm also handles cases where the two collections have gotten\n                // out of sync (typically because exceptions prevented a previous sync\n                // from happening)\n                case NotifyCollectionChangedAction.Reset:\n                    CloneList(clone, origin);\n                    break;\n\n                default:\n                    throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, e.Action));\n            }\n        }\n\n        private void CloneList(IList clone, IList master)\n        {\n            // if either party is null, do nothing.  Allowing null lets the caller\n            // avoid a lazy instantiation of the Sort/Group description collection.\n            if (clone == null || master == null)\n                return;\n\n            if (clone.Count > 0)\n            {\n                clone.Clear();\n            }\n\n            for (int i = 0, n = master.Count; i < n; ++i)\n            {\n                clone.Add(master[i]);","sourceCodeStart":2211,"sourceCodeEnd":2247,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L2211-L2247","documentation":"When ItemCollection syncs its internal clone with the underlying view, it clones the change-log for Add/Remove/Replace/Reset actions. Any other NotifyCollectionChangedAction reaches a default case that throws NotSupportedException(SR.UnexpectedCollectionChangeAction, e.Action) — an internal-invariant guard against malformed change notifications.","triggerScenarios":"A source collection (or custom INotifyCollectionChanged implementation) raising NotifyCollectionChangedEventArgs with an unexpected or invalid Action value that the clone logic does not handle.","commonSituations":"Implementing INotifyCollectionChanged yourself and firing events with wrong/misbuilt args; third-party observable collections that raise nonstandard notifications.","solutions":["Fix the custom INotifyCollectionChanged implementation to raise only valid actions with correctly built EventArgs","Use ObservableCollection or a well-tested observable collection implementation","If you control the event, ensure e.Action is Add, Remove, Replace, or Reset"],"exampleFix":"// before\nRaiseCollectionChanged(new NotifyCollectionChangedEventArgs((NotifyCollectionChangedAction)9));\n// after\nRaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));","handlingStrategy":"try-catch","validationCode":"if (Enum.IsDefined(typeof(NotifyCollectionChangedAction), e.Action) && e.Action != NotifyCollectionChangedAction.Move) { /* handled */ }","typeGuard":"bool isSupportedAction = e.Action is NotifyCollectionChangedAction.Add or NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Replace or NotifyCollectionChangedAction.Reset;","tryCatchPattern":"try { /* sync with view */ } catch (NotSupportedException ex) { /* bad INotifyCollectionChanged source: replace or fix source */ }","preventionTips":["Use ObservableCollection rather than hand-rolled INotifyCollectionChanged","Raise only well-formed NotifyCollectionChangedEventArgs","Test custom collections against ItemsControl consumers"],"tags":["wpf","collection-changed","inotifycollectionchanged"],"backgroundTag":"invalid-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"}