dotnet/wpf · error · ArgumentException

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

Error message

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

What it means

In DocumentSequenceTextContainer.VerifyPosition, after confirming the pointer belongs to this container, the code downcasts to DocumentSequenceTextPointer. If the pointer is valid for this container but is not a DocumentSequenceTextPointer instance (e.g., a generic TextPointer), ArgumentException with BadFixedTextPosition is thrown.

Solutions

  1. Only pass pointers created by the DocumentSequenceTextContainer itself (DocumentSequenceTextPointer instances)
  2. Create positions via ((ITextContainer)seqContainer).Start.CreatePointer(direction)
  3. Catch ArgumentException and recreate the pointer from the sequence container

Example fix

// before
var tp = seqContainer.VerifyPosition(genericPointer);
// after
var tp = seqContainer.VerifyPosition(((ITextContainer)seqContainer).Start.CreatePointer(LogicalDirection.Forward));
Defensive patterns

Strategy: type-guard

Validate before calling

if (position is not DocumentSequenceTextPointer)
    throw new ArgumentException("position must be a DocumentSequenceTextPointer");

Type guard

bool IsSequencePointer(ITextPointer p) => p is DocumentSequenceTextPointer;

Try / catch

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

Prevention

When it happens

Trigger: Passing a non-DocumentSequenceTextPointer ITextPointer implementation into position-comparison APIs on a DocumentSequenceTextContainer; interop code supplying a plain TextPointer or a mock pointer.

Common situations: Custom ITextPointer implementations used in text tests or frameworks interacting with a FixedDocumentSequence; mixed pointer types after refactoring code that used FlowDocument pointers.

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

Appendix: source

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

        //--------------------------------------------------------------------
        // 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)
            {
                if (cdb.ChildContainer == tp.TextContainer)
                {
                    return new DocumentSequenceTextPointer(cdb, tp);
                }
                cdb = cdb.NextBlock;

View on GitHub (pinned to 81131a70a4)