{"record":{"id":"52c38038c53e80f8","repo":"dotnet/wpf","slug":"sr-textviewinvalidlayout-multipagetextview","errorCode":null,"errorMessage":"SR.TextViewInvalidLayout","messagePattern":"SR\\.TextViewInvalidLayout","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/MultiPageTextView.cs","lineNumber":97,"sourceCode":"        //\n        //  Internal Methods\n        //\n        //-------------------------------------------------------------------\n\n        #region Internal Methods\n\n        /// <summary>\n        /// <see cref=\"ITextView.GetTextPositionFromPoint\"/>\n        /// </summary>\n        internal override ITextPointer GetTextPositionFromPoint(Point point, bool snapToText)\n        {\n            ITextPointer position = null;\n            DocumentPageTextView pageTextView;\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\n            pageTextView = GetTextViewFromPoint(point, false);\n            if (pageTextView != null)\n            {\n                // Transform to DocumentPageView coordinates and query inner TextView\n                point = TransformToDescendant(pageTextView.RenderScope, point);\n                position = pageTextView.GetTextPositionFromPoint(point, snapToText);\n            }\n            return position;\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextView.GetRawRectangleFromTextPosition\"/>\n        /// </summary>\n        internal override Rect GetRawRectangleFromTextPosition(ITextPointer position, out Transform transform)\n        {\n            Rect rect = Rect.Empty;","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/MultiPageTextView.cs#L79-L115","documentation":"MultiPageTextView.GetTextPositionFromPoint requires valid layout before mapping a screen point to a text position. When the IsValid property is false the view has no up-to-date layout and cannot answer geometry queries, so it throws InvalidOperationException with SR.TextViewInvalidLayout.","triggerScenarios":"Calling GetTextPositionFromPoint (directly or via BringPointIntoViewAsync, GetPositionAtNextLineCore, GetPositionAtNextPageCore) while MultiPageTextView.IsValid is false — i.e. before layout completes, after layout was invalidated, or while the DocumentPageView has no valid pages.","commonSituations":"Querying the text view from mouse-hit-testing code before the first layout pass finished; calling text-view APIs inside a layout-invalidating handler; pages not yet generated in a paginated DocumentPageView (e.g. before the viewer is rendered).","solutions":["Wait for layout to complete before calling — invoke from LayoutUpdated or after the DocumentPageView reports valid pages.","Check MultiPageTextView.IsValid before the call and skip or defer the operation when false.","Subscribe to the TextView's Updated/LayoutUpdated event and perform the query there.","Force a layout update (UpdateLayout) on the containing viewer prior to the call."],"exampleFix":"// before\nvar position = textView.GetTextPositionFromPoint(point, snapToText); // throws if layout invalid\n\n// after\nif (textView.IsValid)\n{\n    var position = textView.GetTextPositionFromPoint(point, snapToText);\n}","handlingStrategy":"validation","validationCode":"if (!textView.IsValid)\n{\n    return null; // or defer until TextView.Updated fires\n}\nvar position = textView.GetTextPositionFromPoint(point, snapToText);","typeGuard":null,"tryCatchPattern":"try\n{\n    position = textView.GetTextPositionFromPoint(point, snapToText);\n}\ncatch (InvalidOperationException)\n{\n    // layout invalid — defer via Dispatcher or Updated event\n}","preventionTips":["Always check ITextView.IsValid before any geometry/navigation query.","Issue queries from LayoutUpdated or TextView.Updated handlers.","Avoid caching ITextView references across layout invalidations.","Call UpdateLayout() before hit-testing newly loaded documents."],"tags":["wpf","textview","layout"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}