dotnet/wpf · error · NotSupportedException

SR.Format(SR.UnexpectedCollectionChangeAction, args.Action)

Error message

SR.Format(SR.UnexpectedCollectionChangeAction, args.Action)

What it means

In _OnContentChanged, an Add action is only valid when the new DocumentReference was appended at the end of References (startingIndex == References.Count - 1). Any other insert index breaks the sequence's linear block list, so NotSupportedException with UnexpectedCollectionChangeAction is thrown.

Solutions

  1. Only append DocumentReferences to the end of References (use Add)
  2. To insert earlier, rebuild the References collection: remove all, re-add in order, or create a new DocumentSequence
  3. Avoid Insert API on References; treat it as append-only

Example fix

// before
references.Insert(0, newDocRef);
// after
references.Add(newDocRef); // append only
Defensive patterns

Strategy: validation

Validate before calling

if (args.Action == NotifyCollectionChangedAction.Add && args.NewStartingIndex != references.Count - 1)
    throw new NotSupportedException("DocumentSequence.References only supports appending at the end");

Type guard

bool IsAppend(NotifyCollectionChangedEventArgs a, ICollection c) => a.Action == NotifyCollectionChangedAction.Add && a.NewStartingIndex == c.Count - 1;

Try / catch

try { references.Insert(idx, docRef); }
catch (NotSupportedException) { /* rebuild sequence with docRef appended */ }

Prevention

When it happens

Trigger: Inserting a DocumentReference into DocumentSequence.References at an index other than the end (Insert at 0 or middle); raising Add with NewStartingIndex not pointing at the last item.

Common situations: Code that inserts a new page/document at the front of a FixedDocumentSequence; reordering References via Insert instead of Remove+Add at the end.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2e00d28902e27274. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/DocumentSequenceTextContainer.cs:604

        {
#if DEBUG
            this._generation++;
#endif

            if (args.Action == NotifyCollectionChangedAction.Add)
            {
                if (args.NewItems.Count != 1)
                {
                    throw new NotSupportedException(SR.RangeActionsNotSupported);
                }
                else
                {
                        object item = args.NewItems[0];
                        int startingIndex = args.NewStartingIndex;

                        if (startingIndex != _parent.References.Count - 1)
                        {
                            throw new NotSupportedException(SR.Format(SR.UnexpectedCollectionChangeAction, args.Action));
                        }

                        ChildDocumentBlock newBlock = new ChildDocumentBlock(this, (DocumentReference)item);

                        ChildDocumentBlock insertAfter = _doclistTail.PreviousBlock;
                        insertAfter.InsertNextBlock(newBlock);
                        DocumentSequenceTextPointer changeStart =
                            new DocumentSequenceTextPointer(insertAfter, insertAfter.End);

                        //Update end pointer
                        _end = new DocumentSequenceTextPointer(newBlock, newBlock.ChildContainer.End);

                        if (newBlock.NextBlock == _doclistTail && newBlock.PreviousBlock == _doclistHead)
                        {
                            //Update start pointer for the first block
                            _start = new DocumentSequenceTextPointer(newBlock,  newBlock.ChildContainer.Start);
                        }

View on GitHub (pinned to 81131a70a4)