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
- Clamp the end pointer to the view's actual range before calling (verify with view.Contains(end) / Contains(position))
- Re-derive start/end from the current view after any document edit or layout update
- Avoid using container-end pointers; use the view's own end (e.g. obtained from the view range) as the upper bound
- 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
- Clamp the end pointer to the view's range, not the container's
- Prefer view-derived end positions over TextContainer.End
- Re-run layout after document growth before querying
- Test with virtualized/paged documents where view range != document range
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
- Argument out of range (start not contained in view)
- Argument out of range (position does not map to a line)
- ArgumentException.NonNegativeValue (Parameter 'squareScale')
- ArgumentException.PositiveValue (Parameter 'value')
- ArgumentException.PositiveValue (Parameter 'value')
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)