dotnet/wpf · error · ArgumentException

SR.Format(SR.NotInAssociatedContainer, "position")

Error message

SR.Format(SR.NotInAssociatedContainer, "position")

What it means

DocumentSequenceTextContainer.VerifyPosition validates that a given ITextPointer belongs to this container. If position.TextContainer is a different container, it throws ArgumentException with NotInAssociatedContainer. This is an argument-ownership check before downcasting to DocumentSequenceTextPointer.

Solutions

  1. Ensure the pointer was created from the same DocumentSequenceTextContainer (or its Start/End) before passing it
  2. Obtain pointers via documentSequence.TextContainer.Start.CreatePointer(...) rather than from other documents
  3. Wrap position-taking calls in try/catch ArgumentException and re-acquire a fresh pointer

Example fix

// before
seqContainer.VerifyPosition(pointerFromOtherDoc);
// after
if (pointer.TextContainer == seqContainer) { seqContainer.VerifyPosition(pointer); }
Defensive patterns

Strategy: validation

Validate before calling

if (position == null || position.TextContainer != seqTextContainer)
    throw new ArgumentException("position does not belong to this DocumentSequenceTextContainer");

Type guard

bool BelongsToContainer(ITextPointer p, DocumentSequenceTextContainer c) => p != null && p.TextContainer == c;

Try / catch

try { seqContainer.SomePositionApi(pointer); }
catch (ArgumentException) { pointer = ((ITextContainer)seqTextContainer).Start.CreatePointer(); }

Prevention

When it happens

Trigger: Passing a TextPointer created by another document's TextContainer (e.g., a FlowDocument's TextPointer or another DocumentSequence's pointer) into APIs like GetOffsetToPosition, CompareTo, or internal move/verify paths on a DocumentSequenceTextContainer.

Common situations: Mixing pointers across documents when copying content between a FlowDocument and a FixedDocumentSequence; caching a pointer from a previous document instance and reusing it after the document changed.

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/f91474d1e05495a5. Report an issue: GitHub.

Appendix: source

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

        //
        // Internal Methods
        //
        //---------------------------------------------------------------------

        #region Internal Methods

        //--------------------------------------------------------------------
        // Utility Method
        //---------------------------------------------------------------------

        // Verify parameter. Throw Exception if necessary.
        internal DocumentSequenceTextPointer VerifyPosition(ITextPointer position)
        {
            ArgumentNullException.ThrowIfNull(position);

            if (position.TextContainer != this)
            {
                throw new ArgumentException(SR.Format(SR.NotInAssociatedContainer, "position"));
            }

            DocumentSequenceTextPointer tp = position as DocumentSequenceTextPointer;
            if (tp == null)
            {
                throw new ArgumentException(SR.Format(SR.BadFixedTextPosition, "position"));
            }

            return tp;
        }


        // Given an ITextPointer in a child TextContainer, create a position in parent's
        // address space to represent it.
        internal DocumentSequenceTextPointer MapChildPositionToParent(ITextPointer tp)
        {
            ChildDocumentBlock cdb = this._doclistHead;
            while (cdb != null)

View on GitHub (pinned to 81131a70a4)