dotnet/wpf · error · ArgumentOutOfRangeException

Argument out of range (position not contained in view)

Error message

Argument out of range (position not contained in view)

What it means

Within GetRawRectangleFromTextPosition, after layout validity and container checks, ContainsCore(position) verifies the position lies within this view's layout span. If not, ArgumentOutOfRangeException(nameof(position)) is thrown with the message 'Argument out of range (position not contained in view)'.

Solutions

  1. Check Contains(position) (or ContainsCore) before calling and route the position to the correct view.
  2. Use the TextDocumentView associated with the position's containing page/subview.
  3. Handle the exception and fall back to the TextViewService that can locate the right view.
  4. Clamp the position to the view's start/end before querying the rectangle.

Example fix

// before
var rect = view.GetRawRectangleFromTextPosition(pos, out var t);
// after
if (view.Contains(pos))
    var rect = view.GetRawRectangleFromTextPosition(pos, out var t);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!view.Contains(position)) { /* route to the correct view */ }

Type guard

static bool PositionInView(ITextView view, ITextPointer p) => view.Contains(p);

Try / catch

try { rect = view.GetRawRectangleFromTextPosition(position, out transform); }
catch (ArgumentOutOfRangeException) { rect = null; // not in this view }

Prevention

When it happens

Trigger: Passing an ITextPointer that belongs to the same TextContainer but sits outside the range covered by this view's Columns/FloatingElements — e.g. content on another page or beyond a virtualized viewport.

Common situations: Paged documents (FixedDocument/FlowDocument views per page) where a position belongs to a different page view; positions in collapsed or filtered-out content.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextDocumentView.cs:92

        }

        /// <summary>
        /// <see cref="ITextView.GetRawRectangleFromTextPosition"/>
        /// </summary>
        /// <remarks>
        /// TextDocumentView does not calculate any transform for this function. Transform returned is always identity.
        /// </remarks>
        internal override Rect GetRawRectangleFromTextPosition(ITextPointer position, out Transform transform)
        {
            // Verify that layout information is valid. Cannot continue if not valid.
            if (!IsValid)
            {
                throw new InvalidOperationException(SR.TextViewInvalidLayout);
            }
            ValidationHelper.VerifyPosition(_textContainer, position, nameof(position));
            if (!ContainsCore(position))
            {
                throw new ArgumentOutOfRangeException(nameof(position));
            }
            _owner.EnsureValidVisuals();

            Rect rect = GetRectangleFromTextPosition(Columns, FloatingElements, position);

            // Transforms Rect from content's coordinate system.
            TransformFromContent(ref rect, out transform);

            return rect;
        }

        /// <summary>
        /// <see cref="TextViewBase.GetTightBoundingGeometryFromTextPositions"/>
        /// </summary>
        internal override Geometry GetTightBoundingGeometryFromTextPositions(ITextPointer startPosition, ITextPointer endPosition)
        {
            Geometry geometry = null;

View on GitHub (pinned to 81131a70a4)