{"record":{"id":"75312ca41e363a00","repo":"dotnet/wpf","slug":"sr-unexpectedcollectionchangeaction-formatted-with-e-action","errorCode":null,"errorMessage":"SR.UnexpectedCollectionChangeAction (formatted with e.Action)","messagePattern":"SR\\.UnexpectedCollectionChangeAction \\(formatted with e\\.Action\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs","lineNumber":2525,"sourceCode":"                    break;\n\n                case NotifyCollectionChangedAction.Replace:\n                    if (e.NewItems.Count != 1 || e.OldItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    break;\n\n                case NotifyCollectionChangedAction.Move:\n                    if (e.NewItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    if (e.NewStartingIndex < 0)\n                        throw new InvalidOperationException(SR.CannotMoveToUnknownPosition);\n                    break;\n\n                case NotifyCollectionChangedAction.Reset:\n                    break;\n\n                default:\n                    throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, e.Action));\n            }\n        }\n\n\n        /// <summary>\n        /// Create, filter and sort the local index array.\n        /// called from Refresh(), override in derived classes as needed.\n        /// </summary>\n        /// <param name=\"list\">new ILIst to associate this view with</param>\n        /// <returns>new local array to use for this view</returns>\n        private void PrepareLocalArray()\n        {\n            PrepareShaping();\n\n            LiveShapingList lsList = _internalList as LiveShapingList;\n            if (lsList != null)\n            {\n                lsList.LiveShapingDirty -= new EventHandler(OnLiveShapingDirty);","sourceCodeStart":2507,"sourceCodeEnd":2543,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/ListCollectionView.cs#L2507-L2543","documentation":"ValidateCollectionChangedEventArgs's default branch throws NotSupportedException with SR.UnexpectedCollectionChangeAction when e.Action is not one of the five standard NotifyCollectionChangedAction values. Only Add/Remove/Replace/Move/Reset are valid; any other value means a broken or custom event source.","triggerScenarios":"A bound INotifyCollectionChanged source raises CollectionChanged with an undefined enum value (e.g. an out-of-range cast) that reaches the validation switch's default case.","commonSituations":"Unsafe enum casts in custom collections, code generators or interop layers fabricating NotifyCollectionChangedEventArgs, corrupted event replay/logging frameworks.","solutions":["Audit the event-raising code so only NotifyCollectionChangedAction.Add/Remove/Replace/Move/Reset are used.","Replace raw enum casts with the static NotifyCollectionChangedEventArgs factory methods.","Catch NotSupportedException around updates and force a view rebuild via Refresh()."],"exampleFix":"// before\nvar args = new NotifyCollectionChangedEventArgs((NotifyCollectionChangedAction)rawValue);\n// after\nvar action = Enum.IsDefined(typeof(NotifyCollectionChangedAction), rawValue)\n    ? (NotifyCollectionChangedAction)rawValue\n    : NotifyCollectionChangedAction.Reset;","handlingStrategy":"type-guard","validationCode":"if (!Enum.IsDefined(typeof(NotifyCollectionChangedAction), args.Action)) { /* reject or fall back to Reset */ }","typeGuard":"bool IsValidAction(NotifyCollectionChangedAction a) => a is NotifyCollectionChangedAction.Add or NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Replace or NotifyCollectionChangedAction.Move or NotifyCollectionChangedAction.Reset;","tryCatchPattern":"try { /* process event */ } catch (NotSupportedException) { view.Refresh(); }","preventionTips":["Use the NotifyCollectionChangedEventArgs factory methods only","Validate raw values with Enum.IsDefined before casting","Add unit tests asserting only standard actions are raised"],"tags":["wpf","collection-view","invalid-enum"],"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"}