dotnet/wpf · error · InvalidOperationException

SR.Format(SR.TextElementCollection_PreviousSiblingDoesNotBel…

Error message

SR.Format(SR.TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection, previousSibling.GetType().Name)

What it means

TextElementCollection<T>.InsertAfter requires the previousSibling to belong to the same collection (same Parent). If previousSibling.Parent differs from this collection's Parent, it throws InvalidOperationException saying the previous sibling does not belong to this collection.

Solutions

  1. Use a previousSibling that is actually a member of the target collection (check its Parent first).
  2. Move previousSibling into the target collection first, then InsertAfter.
  3. Use Add/AddBefore on the correct collection instead.

Example fix

// before
targetInlines.InsertAfter(newItem, itemFromOtherParagraph);
// after
if (itemFromOtherParagraph.Parent == targetParagraph)
    targetInlines.InsertAfter(newItem, itemFromOtherParagraph);
else
    targetInlines.Add(newItem);
Defensive patterns

Strategy: validation

Validate before calling

bool siblingInCollection = previousSibling != null && previousSibling.Parent == targetParent;
if (!siblingInCollection) throw new ArgumentException("previousSibling must belong to the target collection");

Try / catch

try { targetInlines.InsertAfter(newItem, previousSibling); }
catch (InvalidOperationException) { targetInlines.Add(newItem); }

Prevention

When it happens

Trigger: Calling InsertAfter(newItem, previousSibling) where previousSibling lives under a different parent element (e.g. inserting into Paragraph B's Inlines using an element from Paragraph A).

Common situations: Copy/paste and drag-drop code moving elements between FlowDocument containers; reordering logic that assumes a sibling is in the target collection.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElementCollection.cs:248

        /// <summary>
        /// Inserts a unlinked TextElement newItem after previousSibling, which is supposed to be an existing member of this collection.
        /// </summary>
        /// <param name="previousSibling">
        /// TextElement after which the newItem is to be inserted
        /// </param>
        /// <param name="newItem">
        /// A TextElement to be inserted into the collection after the previousSibling.
        /// It must be unlinked from a tree before insertion.
        /// </param>
        public void InsertAfter(TextElementType previousSibling, TextElementType newItem)
        {
            ArgumentNullException.ThrowIfNull(previousSibling);

            ArgumentNullException.ThrowIfNull(newItem);

            if (previousSibling.Parent != this.Parent)
            {
                throw new InvalidOperationException(SR.Format(SR.TextElementCollection_PreviousSiblingDoesNotBelongToThisCollection, previousSibling.GetType().Name));
            }

            if (newItem.Parent != null)
            {
                throw new ArgumentException(SR.Format(SR.TextSchema_TheChildElementBelongsToAnotherTreeAlready, this.GetType().Name));
            }

            ValidateChild(newItem);

            this.TextContainer.BeginChange();
            try
            {
                newItem.RepositionWithContent(previousSibling.ElementEnd);
            }
            finally
            {
                this.TextContainer.EndChange();
            }

View on GitHub (pinned to 81131a70a4)