{"record":{"id":"ec49cf7af51f7375","repo":"dotnet/wpf","slug":"argument-out-of-range-end-not-contained-in-view","errorCode":null,"errorMessage":"Argument out of range (end not contained in view)","messagePattern":"Argument out of range \\(end not contained in view\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextDocumentView.cs","lineNumber":371,"sourceCode":"        internal override ReadOnlyCollection<GlyphRun> GetGlyphRuns(ITextPointer start, ITextPointer end)\n        {\n            List<GlyphRun> glyphRuns = new List<GlyphRun>();\n\n            // Verify that layout information is valid. Cannot continue if not valid.\n            if (!IsValid)\n            {\n                throw new InvalidOperationException(SR.TextViewInvalidLayout);\n            }\n            ValidationHelper.VerifyPosition(_textContainer, start, nameof(start));\n            ValidationHelper.VerifyPosition(_textContainer, end, nameof(end));\n            ValidationHelper.VerifyPositionPair(start, end);\n            if (!ContainsCore(start))\n            {\n                throw new ArgumentOutOfRangeException(nameof(start));\n            }\n            if (!ContainsCore(end))\n            {\n                throw new ArgumentOutOfRangeException(nameof(end));\n            }\n\n            GetGlyphRuns(glyphRuns, start, end, Columns, FloatingElements);\n\n            return new ReadOnlyCollection<GlyphRun>(glyphRuns);\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextView.Contains\"/>\n        /// </summary>\n        internal override bool Contains(ITextPointer position)\n        {\n            // Verify that layout information is valid. Cannot continue if not valid.\n            if (!IsValid)\n            {\n                throw new InvalidOperationException(SR.TextViewInvalidLayout);\n            }\n            ValidationHelper.VerifyPosition(_textContainer, position, nameof(position));","sourceCodeStart":353,"sourceCodeEnd":389,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextDocumentView.cs#L353-L389","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nvar glyphs = view.GetGlyphRuns(start, textContainer.End);\n// after\nITextPointer end = view.Contains(textContainer.End)\n    ? textContainer.End\n    : view.GetTextPositionFromPoint(new Point(double.PositiveInfinity, double.PositiveInfinity), true);\nvar glyphs = view.GetGlyphRuns(start, end);","handlingStrategy":"validation","validationCode":"if (view.Contains(end)) { var runs = view.GetGlyphRuns(start, end); }","typeGuard":"bool EndInInView(TextDocumentView view, ITextPointer end) => view != null && view.Contains(end);","tryCatchPattern":"try { var runs = view.GetGlyphRuns(start, end); }\ncatch (ArgumentOutOfRangeException) { /* clamp end via view.GetTextPositionFromPoint(maxPoint, true) and retry */ }","preventionTips":["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"],"tags":["wpf","textlayout","argumentoutofrange"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}