{"record":{"id":"d7ef28e729a2acb5","repo":"dotnet/wpf","slug":"sr-rangeactionsnotsupported","errorCode":null,"errorMessage":"SR.RangeActionsNotSupported","messagePattern":"SR\\.RangeActionsNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs","lineNumber":2392,"sourceCode":"                if (index < 0)\n                    throw new InvalidOperationException(SR.Format(SR.CollectionAddEventMissingItem, item));\n            }\n        }\n\n        /// <summary>\n        /// Forward a CollectionChanged event\n        /// </summary>\n        // Called  when items collection changes.\n        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)\n        {\n            if (sender != ItemsInternal && args.Action != NotifyCollectionChangedAction.Reset)\n                return;     // ignore events (except Reset) from ItemsCollection when we're listening to group's items.\n\n            switch (args.Action)\n            {\n                case NotifyCollectionChangedAction.Add:\n                    if (args.NewItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    OnItemAdded(args.NewItems[0], args.NewStartingIndex);\n                    break;\n\n                case NotifyCollectionChangedAction.Remove:\n                    if (args.OldItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    OnItemRemoved(args.OldItems[0], args.OldStartingIndex);\n                    break;\n\n                case NotifyCollectionChangedAction.Replace:\n                    // Don't check arguments if app targets 4.0, for compat ( 726682)\n                    if (!FrameworkCompatibilityPreferences.TargetsDesktop_V4_0)\n                    {\n                        if (args.OldItems.Count != 1)\n                            throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    }\n                    OnItemReplaced(args.OldItems[0], args.NewItems[0], args.NewStartingIndex);\n                    break;","sourceCodeStart":2374,"sourceCodeEnd":2410,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs#L2374-L2410","documentation":"ItemContainerGenerator's OnCollectionChanged only supports single-item Add and Remove notifications. If an event reports multiple items (NewItems.Count or OldItems.Count != 1) for those actions, it throws NotSupportedException SR.RangeActionsNotSupported, because range change actions are not supported by this generator's incremental update logic.","triggerScenarios":"A source INotifyCollectionChanged collection raises Add or Remove with several items at once (multi-item range events), e.g. AddRange-like extensions that batch notifications.","commonSituations":"Custom ObservableCollection subclasses implementing AddRange with a single multi-item event instead of N single-item events or a Reset; bulk load/unload operations.","solutions":["Raise one Add/Remove event per item instead of a multi-item range event.","Raise NotifyCollectionChangedAction.Reset for bulk changes so the generator rebuilds.","Replace custom AddRange with sequential Add calls, or use a collection that emits Reset on bulk operations."],"exampleFix":"// before\nOnCollectionChanged(new NotifyCollectionChangedEventArgs(Add, newItemsList, startIndex)); // multi-item\n\n// after\nforeach (var item in newItemsList)\n{\n    items.Add(item);\n    OnCollectionChanged(new NotifyCollectionChangedEventArgs(Add, item, items.IndexOf(item)));\n}\n// or: OnCollectionChanged(new NotifyCollectionChangedEventArgs(Reset));","handlingStrategy":"validation","validationCode":"// Guard in your collection wrapper before raising:\nif (action == NotifyCollectionChangedAction.Add && args.NewItems.Count != 1)\n    throw new NotSupportedException(\"ItemContainerGenerator supports only single-item Add; use Reset for bulk changes\");","typeGuard":"static bool IsSingleItemChange(NotifyCollectionChangedEventArgs a) =>\n    (a.Action == NotifyCollectionChangedAction.Add && a.NewItems.Count == 1) ||\n    (a.Action == NotifyCollectionChangedAction.Remove && a.OldItems.Count == 1);","tryCatchPattern":"try { /* bind collection */ }\ncatch (NotSupportedException) { /* replace source with a Reset-emitting adapter */ }","preventionTips":["Never raise multi-item Add/Remove events toward ItemContainerGenerator.","Implement AddRange as sequential single-item Adds or a final Reset.","Wrap third-party range-event collections in an adapter that emits Reset."],"tags":["wpf","itemcontainergenerator","collection-changed","not-supported"],"backgroundTag":"unsupported-operation","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"}