{"record":{"id":"ca71593b407d5bff","repo":"dotnet/wpf","slug":"sr-rangeactionsnotsupported-documentsequencetextcontainer","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/Documents/DocumentSequenceTextContainer.cs","lineNumber":595,"sourceCode":"            {\n                textContainer.EndChange();\n            }\n        }\n\n        //--------------------------------------------------------------------\n        // ContentChange\n        //---------------------------------------------------------------------\n        private void _OnContentChanged(object sender, NotifyCollectionChangedEventArgs args)\n        {\n#if DEBUG\n            this._generation++;\n#endif\n\n            if (args.Action == NotifyCollectionChangedAction.Add)\n            {\n                if (args.NewItems.Count != 1)\n                {\n                    throw new NotSupportedException(SR.RangeActionsNotSupported);\n                }\n                else\n                {\n                        object item = args.NewItems[0];\n                        int startingIndex = args.NewStartingIndex;\n\n                        if (startingIndex != _parent.References.Count - 1)\n                        {\n                            throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, args.Action));\n                        }\n\n                        ChildDocumentBlock newBlock = new ChildDocumentBlock(this, (DocumentReference)item);\n\n                        ChildDocumentBlock insertAfter = _doclistTail.PreviousBlock;\n                        insertAfter.InsertNextBlock(newBlock);\n                        DocumentSequenceTextPointer changeStart =\n                            new DocumentSequenceTextPointer(insertAfter, insertAfter.End);\n","sourceCodeStart":577,"sourceCodeEnd":613,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextContainer.cs#L577-L613","documentation":"DocumentSequenceTextContainer._OnContentChanged handles CollectionChanged from the sequence's References. Add events must contain exactly one item; a multi-item (range) Add is not supported by the incremental ChildDocumentBlock bookkeeping, so NotSupportedException with RangeActionsNotSupported is thrown.","triggerScenarios":"Adding multiple DocumentReference items to DocumentSequence.References in one CollectionChanged event (e.g., AddRange or a collection that batches adds).","commonSituations":"Custom ObservableCollection subclass that raises one Add event with several new items; batch-loading child documents into a FixedDocumentSequence via a bulk-add helper.","solutions":["Add DocumentReference items one at a time so each Add event carries a single item","Replace bulk AddRange with a loop of individual Add calls","If you own the collection, never batch NotifyCollectionChangedAction.Add with multiple NewItems"],"exampleFix":"// before\nreferences.AddRange(newDocs); // single Add event, count != 1\n// after\nforeach (var d in newDocs) { references.Add(d); }","handlingStrategy":"validation","validationCode":"if (args.Action == NotifyCollectionChangedAction.Add && args.NewItems.Count != 1)\n    throw new NotSupportedException(\"DocumentSequence.References supports single-item Add only\");","typeGuard":"bool IsSingleItemAdd(NotifyCollectionChangedEventArgs a) => a.Action == NotifyCollectionChangedAction.Add && a.NewItems?.Count == 1;","tryCatchPattern":"try { references.AddRange(items); }\ncatch (NotSupportedException) { foreach (var i in items) references.Add(i); }","preventionTips":["Never batch-add multiple DocumentReferences in one event","Replace AddRange with per-item Add loops","Keep References a simple ObservableCollection with single-item notifications"],"tags":["wpf","documents","collections","range-not-supported"],"backgroundTag":"unsupported-collection-change-action","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"}