{"record":{"id":"20c746c73dd2b093","repo":"dotnet/wpf","slug":"sr-format-sr-unexpectedcollectionchangeaction-e-action-20c746","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/Data/CollectionView.cs","lineNumber":1978,"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        // fix up CurrentPosition and CurrentItem after a collection change\n        private void AdjustCurrencyForAdd(int index)\n        {\n            // adjust current index if insertion is earlier\n            if (Count == 1)\n                _currentPosition = -1;\n            else if (index <= _currentPosition)\n            {\n                ++_currentPosition;\n\n                if (_currentPosition < Count)\n                {\n                    _currentItem = EnumerableWrapper[_currentPosition];\n                }\n            }","sourceCodeStart":1960,"sourceCodeEnd":1996,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/CollectionView.cs#L1960-L1996","documentation":"CollectionView.ProcessCollectionChanged handles Add, Remove, Replace, Move and Reset. Any other NotifyCollectionChangedAction value reaching it falls into the default case and throws NotSupportedException with UnexpectedCollectionChangeAction. Since the enum has no other standard values, this guards against corrupt or synthetic events.","triggerScenarios":"A NotifyCollectionChangedEventArgs with an out-of-range or undefined action value is passed to the view, e.g. by casting an arbitrary int to NotifyCollectionChangedAction or via reflection-based event raising.","commonSituations":"Testing frameworks or mocking libraries fabricating change events with invalid enum values; binary/formatter deserialization producing corrupt enum values; hand-written event args with an uninitialized enum field (0 if not assigned to a valid action).","solutions":["Only raise events with valid NotifyCollectionChangedAction values (Add, Remove, Replace, Move, Reset)","Audit custom INotifyCollectionChanged implementations for uninitialized enum fields","Validate enum values before constructing NotifyCollectionChangedEventArgs"],"exampleFix":"// before\nvar action = (NotifyCollectionChangedAction)rawValue; // may be invalid\n// after\nif (Enum.IsDefined(typeof(NotifyCollectionChangedAction), rawValue))\n    var action = (NotifyCollectionChangedAction)rawValue;","handlingStrategy":"validation","validationCode":"bool isValidAction(NotifyCollectionChangedAction a) => a is NotifyCollectionChangedAction.Add or NotifyCollectionChangedAction.Remove or NotifyCollectionChangedAction.Replace or NotifyCollectionChangedAction.Move or NotifyCollectionChangedAction.Reset;","typeGuard":"static bool IsDefinedAction(NotifyCollectionChangedAction a) => Enum.IsDefined(typeof(NotifyCollectionChangedAction), a);","tryCatchPattern":"try { view.ProcessChange(e); } catch (NotSupportedException ex) when (ex.Message.Contains(\"UnexpectedCollectionChangeAction\")) { view.Refresh(); }","preventionTips":["Initialize enum fields explicitly to a valid action","Cast to NotifyCollectionChangedAction only after Enum.IsDefined","Favor well-tested collection base classes over manual event raising"],"tags":["wpf","data-binding","collectionview","invalid-enum-value"],"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"}