{"record":{"id":"df596a478e1df169","repo":"dotnet/wpf","slug":"throw-new-notsupportedexception-sr-rangeactionsnotsupported","errorCode":null,"errorMessage":"throw new NotSupportedException(SR.RangeActionsNotSupported);","messagePattern":"throw new NotSupportedException\\(SR\\.RangeActionsNotSupported\\);","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs","lineNumber":1434,"sourceCode":"        // error CS0649: Warning as Error: Field '..._traceLog' is never assigned to, and will always have its default value null\n        private void InitializeTraceLog()\n        {\n            _traceLog = new TraceLog(20);\n        }\n\n        private void TraceContainerCollectionChange(object sender, NotifyCollectionChangedAction action, object oldItem, object newItem)\n        {\n            _traceLog?.Add(\"ContainerCollectionChange from {0}  action = {1} oldItem = {2} newItem = {3}\",\n                                TraceLog.IdFor(sender), action, TraceLog.IdFor(oldItem), TraceLog.IdFor(newItem));\n        }\n\n        private void ValidateCollectionChangedEventArgs(NotifyCollectionChangedEventArgs e)\n        {\n            switch (e.Action)\n            {\n                case NotifyCollectionChangedAction.Add:\n                    if (e.NewItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    break;\n\n                case NotifyCollectionChangedAction.Remove:\n                    if (e.OldItems.Count != 1)\n                        throw new NotSupportedException(SR.RangeActionsNotSupported);\n                    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;","sourceCodeStart":1416,"sourceCodeEnd":1452,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/CompositeCollectionView.cs#L1416-L1452","documentation":"CompositeCollectionView's ValidateCollectionChangedEventArgs rejects Add notifications that carry more than one new item. Range Add actions are not supported by this view, so a multi-item Add throws NotSupportedException.","triggerScenarios":"A source collection raises CollectionChanged with Action=Add and NewItems.Count > 1 (e.g. AddRange implementations that batch multiple items into one notification).","commonSituations":"Using custom 'AddRange' observable collections that fire a single batched Add event; interop with sources that emit range notifications.","solutions":["Raise one Add notification per item instead of a batched range Add.","If batch semantics are needed, fall back to Reset only when the collection is fully cleared (see CompositeCollectionResetOnlyOnClear).","Use ObservableCollection's single-item Add (or insert items in a loop) rather than a range Add helper."],"exampleFix":"// before\nvar e = new NotifyCollectionChangedEventArgs(Add, changedItems.ToList()); // range\n\n// after\nforeach (var item in changedItems)\n    OnCollectionChanged(new NotifyCollectionChangedEventArgs(Add, item));","handlingStrategy":"validation","validationCode":"if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems.Count != 1)\n    SplitIntoSingleItemNotifications(e);","typeGuard":null,"tryCatchPattern":"try { view.ProcessCollectionChanged(e); }\ncatch (NotSupportedException) { RebuildView(); }","preventionTips":["Avoid AddRange-style helpers that emit batched Add notifications to WPF views.","Raise one event per item for Add/Remove/Replace/Move.","Prefer ObservableCollection or INotifyCollectionChanged sources that emit single-item events."],"tags":["wpf","collection-view","range-actions"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}