{"record":{"id":"a1b5359b2e3a2693","repo":"dotnet/wpf","slug":"sr-textcontainerchangingreentrancyinvalid-textblock","errorCode":null,"errorMessage":"SR.TextContainerChangingReentrancyInvalid","messagePattern":"SR\\.TextContainerChangingReentrancyInvalid","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs","lineNumber":389,"sourceCode":"        /// The closest TextPointer to the supplied Point, or null if\n        /// snapToText is false and the supplied Point is not contained\n        /// within any character bounding box.\n        /// </returns>\n        /// <exception cref=\"System.InvalidOperationException\">\n        /// Throws InvalidOperationException if layout is dirty.\n        /// </exception>\n        /// <remarks>\n        /// The TextPointer returned always has its IsFrozen property set true\n        /// and LogicalDirection property set towards a character hit by the point,\n        /// or closest to this point.\n        /// </remarks>\n        public TextPointer GetPositionFromPoint(Point point, bool snapToText)\n        {\n            TextPointer position;\n\n            if(CheckFlags(Flags.ContentChangeInProgress))\n            {\n                throw new InvalidOperationException(SR.TextContainerChangingReentrancyInvalid);\n            }\n\n            EnsureComplexContent();\n\n            // Validate layout information on TextView.\n            if (((ITextView)_complexContent.TextView).Validate(point))\n            {\n                position = (TextPointer)_complexContent.TextView.GetTextPositionFromPoint(point, snapToText);\n            }\n            else\n            {\n                position = snapToText ? new TextPointer((TextPointer)_complexContent.TextContainer.Start) : null;\n            }\n\n            // BUG: We must freeze a pointer before returning.\n\n            return position;\n        }","sourceCodeStart":371,"sourceCodeEnd":407,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextBlock.cs#L371-L407","documentation":"TextBlock.GetPositionFromPoint throws InvalidOperationException while a content change is in progress on the text container. Reentrancy during a TextContainer change (e.g. inside a TextChanged handler) would corrupt layout state, so the API refuses the call until the change completes.","triggerScenarios":"Calling GetPositionFromPoint from within a TextChanged/TextChanged-like event raised during an ongoing text container change; hit-testing during TextContainer.BeginChange/EndChange window.","commonSituations":"Custom caret/hover logic wired to TextChanged; automation hit-testing triggered synchronously by content edits; layout events re-entering the TextBlock mid-change.","solutions":["Defer the call: Dispatcher.BeginInvoke(DispatcherPriority.Input, () => textBlock.GetPositionFromPoint(...)).","Perform hit-testing outside change events, e.g. on mouse events instead of TextChanged.","Check TextBlock content-change state before calling if you control the change scope."],"exampleFix":"// before\nvoid OnTextChanged(object s, TextChangedEventArgs e) =>\n    var p = textBlock.GetPositionFromPoint(mousePoint, false);\n// after\nvoid OnTextChanged(object s, TextChangedEventArgs e) =>\n    Dispatcher.BeginInvoke(DispatcherPriority.Input,\n        new Action(() => textBlock.GetPositionFromPoint(mousePoint, false)));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var p = textBlock.GetPositionFromPoint(point, snapToText); }\ncatch (InvalidOperationException) { Dispatcher.BeginInvoke(() => textBlock.GetPositionFromPoint(point, snapToText)); }","preventionTips":["Never hit-test inside TextChanged handlers","Defer with Dispatcher.BeginInvoke during edits","Prefer mouse/pointer events for point queries"],"tags":["wpf","textblock","reentrancy","invalid-operation"],"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"}