{"record":{"id":"b80cc5d99ed4614e","repo":"dotnet/wpf","slug":"sr-textviewinvalidlayout-textdocumentview","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/TextDocumentView.cs","lineNumber":64,"sourceCode":"        #endregion Constructors\n\n        //-------------------------------------------------------------------\n        //\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            // Verify that layout information is valid. Cannot continue if not valid.\n            if (!IsValid)\n            {\n                throw new InvalidOperationException(SR.TextViewInvalidLayout);\n            }\n\n            _owner.EnsureValidVisuals();\n\n            // Transforms point to content's coordinate system.\n            TransformToContent(ref point);\n\n            // Search columns\n            return GetTextPositionFromPoint(Columns, FloatingElements, point, snapToText);\n        }\n\n        /// <summary>\n        /// <see cref=\"ITextView.GetRawRectangleFromTextPosition\"/>\n        /// </summary>\n        /// <remarks>\n        /// TextDocumentView does not calculate any transform for this function. Transform returned is always identity.\n        /// </remarks>\n        internal override Rect GetRawRectangleFromTextPosition(ITextPointer position, out Transform transform)","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextDocumentView.cs#L46-L82","documentation":"TextDocumentView.GetTextPositionFromPoint requires a valid layout. If IsValid is false (layout is out of date or not computed), it throws InvalidOperationException(SR.TextViewInvalidLayout) because hit-testing against stale or missing layout would return wrong positions.","triggerScenarios":"Calling GetTextPositionFromPoint after the document content changed but before layout was updated, or after the view was invalidated/disposed (e.g. document unloaded, control not yet rendered).","commonSituations":"Hit-testing a TextEditor/TextBox view during or immediately after editing, before a layout pass; querying positions in a window not yet shown; automation code running on the UI thread before measure/arrange.","solutions":["Ensure a layout pass has occurred (call UpdateLayout or wait for Loaded/rendered) before hit-testing.","Check the view's IsValid property first and skip/retry when false.","Subscribe to layout-updated/activated events from the text view and only query when valid.","Call from the UI thread after the editor reports layout validity (e.g. TextView.LayoutUpdated)."],"exampleFix":"// before\nvar pos = textView.GetTextPositionFromPoint(point, true);\n// after\nif (textView.IsValid)\n    var pos = textView.GetTextPositionFromPoint(point, true);","handlingStrategy":"type-guard","validationCode":"if (!textView.IsValid) return null;","typeGuard":"static bool CanHitTest(ITextView view) => view is { IsValid: true };","tryCatchPattern":"try { return view.GetTextPositionFromPoint(point, snapToText); }\ncatch (InvalidOperationException) { return null; // layout not ready }","preventionTips":["Only query the view after a completed layout pass (UpdateLayout / Loaded).","Check IsValid before every view query.","Subscribe to the view's layout-updated event instead of polling after edits.","Avoid hit-testing in constructors or before the control is rendered."],"tags":["wpf","invalidoperationexception","layout","textview"],"backgroundTag":"invalid-layout-state","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"}