dotnet/wpf · error · ArgumentOutOfRangeException

Argument out of range (end not contained in view)

Error message

Argument out of range (end not contained in view)

What it means

TextDocumentView.GetGlyphRuns validates that the end position lies within the view's laid-out range. This error means the end ITextPointer is valid in the text container but points beyond the region this view actually rendered. Only glyph runs inside the view's range can be enumerated.

Solutions

  1. Clamp the end pointer to the view's actual range before calling (verify with view.Contains(end) / Contains(position))
  2. Re-derive start/end from the current view after any document edit or layout update
  3. Avoid using container-end pointers; use the view's own end (e.g. obtained from the view range) as the upper bound
  4. Catch ArgumentOutOfRangeException and fall back to querying a smaller in-view range

Example fix

// before
var glyphs = view.GetGlyphRuns(start, textContainer.End);
// after
ITextPointer end = view.Contains(textContainer.End)
    ? textContainer.End
    : view.GetTextPositionFromPoint(new Point(double.PositiveInfinity, double.PositiveInfinity), true);
var glyphs = view.GetGlyphRuns(start, end);
Defensive patterns

Strategy: validation

Validate before calling

if (view.Contains(end)) { var runs = view.GetGlyphRuns(start, end); }

Type guard

bool EndInInView(TextDocumentView view, ITextPointer end) => view != null && view.Contains(end);

Try / catch

try { var runs = view.GetGlyphRuns(start, end); }
catch (ArgumentOutOfRangeException) { /* clamp end via view.GetTextPositionFromPoint(maxPoint, true) and retry */ }

Prevention

When it happens

Trigger: Calling GetGlyphRuns with an end pointer located after the last laid-out character (e.g. the document's end pointer when the view only covers a subset, or an end in a collapsed/never-measured region). The end passes VerifyPosition but fails ContainsCore(end).

Common situations: Typical when clipping to DocumentEnd or TextContainer.End while the view represents only visible text (virtualized/paged documents), or when the document grew after the pointers were captured and layout has not caught up.

Related errors


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

Appendix: source

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

        internal override ReadOnlyCollection<GlyphRun> GetGlyphRuns(ITextPointer start, ITextPointer end)
        {
            List<GlyphRun> glyphRuns = new List<GlyphRun>();

            // Verify that layout information is valid. Cannot continue if not valid.
            if (!IsValid)
            {
                throw new InvalidOperationException(SR.TextViewInvalidLayout);
            }
            ValidationHelper.VerifyPosition(_textContainer, start, nameof(start));
            ValidationHelper.VerifyPosition(_textContainer, end, nameof(end));
            ValidationHelper.VerifyPositionPair(start, end);
            if (!ContainsCore(start))
            {
                throw new ArgumentOutOfRangeException(nameof(start));
            }
            if (!ContainsCore(end))
            {
                throw new ArgumentOutOfRangeException(nameof(end));
            }

            GetGlyphRuns(glyphRuns, start, end, Columns, FloatingElements);

            return new ReadOnlyCollection<GlyphRun>(glyphRuns);
        }

        /// <summary>
        /// <see cref="ITextView.Contains"/>
        /// </summary>
        internal override bool Contains(ITextPointer position)
        {
            // Verify that layout information is valid. Cannot continue if not valid.
            if (!IsValid)
            {
                throw new InvalidOperationException(SR.TextViewInvalidLayout);
            }
            ValidationHelper.VerifyPosition(_textContainer, position, nameof(position));

View on GitHub (pinned to 81131a70a4)