dotnet/wpf · error · ArgumentException

SR.NotInAssociatedContainer

Error message

SR.NotInAssociatedContainer

What it means

FixedTextContainer.VerifyPosition validates that an ITextPointer argument belongs to this FixedTextContainer. If position.TextContainer is a different container, an ArgumentException(SR.NotInAssociatedContainer) is thrown — the pointer is not associated with the container the API operates on.

Solutions

  1. Obtain the pointer from the same FixedTextContainer you pass it to (e.g. container.CreatePointerAtOffset).
  2. Track which document/container each pointer came from; do not mix pointers between documents.
  3. Re-create the pointer after reloading or rebuilding the document.
  4. Catch ArgumentException and create a fresh pointer within the target container.

Example fix

// before
var ftp = otherDocument.TextContainer.CreatePointerAtOffset(0, direction);
targetContainer.VerifyPosition(ftp); // throws
// after
var ftp = targetContainer.CreatePointerAtOffset(0, direction);
targetContainer.VerifyPosition(ftp);
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsFromContainer(ITextPointer p, FixedTextContainer c) => p != null && p.TextContainer == c;

Type guard

bool CanVerify(ITextPointer p, FixedTextContainer c) => p?.TextContainer == c;

Try / catch

try { container.VerifyPosition(pointer); }
catch (ArgumentException) { pointer = container.CreatePointerAtOffset(0, LogicalDirection.Forward); }

Prevention

When it happens

Trigger: Calling a FixedTextContainer/FixedDocument API (e.g. text editor methods, VerifyPosition) passing an ITextPointer obtained from a different document or container.

Common situations: Mixing text pointers across two FixedDocument/FixedPage instances; caching a pointer from a document that was reloaded; passing a TextPointer from a FlowDocument into a FixedDocument API.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextContainer.cs:313

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

        #region Internal Methods

        //--------------------------------------------------------------------
        // Utility Method
        //---------------------------------------------------------------------
        internal FixedTextPointer VerifyPosition(ITextPointer position)
        {
            ArgumentNullException.ThrowIfNull(position);

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

            FixedTextPointer ftp = position as FixedTextPointer;

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

            return ftp;
        }


        internal int GetPageNumber(ITextPointer  textPointer)
        {
            FixedTextPointer fixedTextPointer = textPointer as FixedTextPointer;
            int  pageNumber = int.MaxValue;

View on GitHub (pinned to 81131a70a4)