{"record":{"id":"e10997e3279a69a8","repo":"dotnet/wpf","slug":"sr-noscopingelement-this-textnavigator","errorCode":null,"errorMessage":"SR.NoScopingElement (This TextNavigator)","messagePattern":"SR\\.NoScopingElement \\(This TextNavigator\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":2640,"sourceCode":"\n            _tree.EmptyDeadPositionList();\n\n            SyncToTreeGeneration();\n\n            TextTreeNode scopingNode = GetScopingNode();\n            elementNode = scopingNode as TextTreeTextElementNode;\n            if (elementNode == null)\n            {\n                // if we're at the root of the tree, the pointer is\n                // already at the element edge, and nothing more need be done.\n                // This case can arise when a text tree contains only a\n                // BlockUIContainer (and no text)\n                if (scopingNode is TextTreeRootNode)\n                {\n                    return;\n                }\n\n                throw new InvalidOperationException(SR.Format(SR.NoScopingElement, \"This TextNavigator\"));\n            }\n\n            MoveToNode(_tree, elementNode, edge);\n        }\n\n        // <see cref=\"TextPointer.MoveToLineBoundary\"/>\n        int ITextPointer.MoveToLineBoundary(int count)\n        {\n            return MoveToLineBoundary(count);\n        }\n\n        // <see cref=\"TextPointer.GetCharacterRect\"/>\n        Rect ITextPointer.GetCharacterRect(LogicalDirection direction)\n        {\n            return GetCharacterRect(direction);\n        }\n\n        bool ITextPointer.MoveToInsertionPosition(LogicalDirection direction)","sourceCodeStart":2622,"sourceCodeEnd":2658,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L2622-L2658","documentation":"This TextPointer method (an internal move/validate helper, e.g. MoveToContentBoundary's validation) requires an element node scoping the pointer. After syncing to the tree generation, if the scoping node is neither a TextElement-derived node nor the tree root, it throws InvalidOperationException(SR.NoScopingElement, \"This TextNavigator\") because the pointer has no valid scoping element in the current tree.","triggerScenarios":"Invoking the public TextPointer move/validation API on a pointer whose scoping node is no longer a TextElement (e.g. after the surrounding element was removed by an edit, or the pointer belongs to a stale/detached TextContainer generation).","commonSituations":"Holding a TextPointer across document restructure (blocks cleared/replaced) and then using it; pointers captured before asynchronous UI updates; using pointers from one RichTextBox's document against another.","solutions":["Re-acquire the TextPointer from the current document (ContentStart/ContentEnd/selection) instead of reusing stale pointers.","Check that the pointer's Parent is a TextElement before moving; if not, discard or re-create the pointer.","Catch InvalidOperationException, re-derive the pointer, and retry the operation."],"exampleFix":"// before\nsavedPointer.MoveToPosition(otherPointer, LogicalDirection.Forward);\n// after\nif (savedPointer.Parent is TextElement)\n    savedPointer.MoveToPosition(otherPointer, LogicalDirection.Forward);\nelse\n    savedPointer = richTextBox.Document.ContentStart;","handlingStrategy":"validation","validationCode":"if (!(pointer.Parent is TextElement))\n    pointer = document.ContentStart.GetInsertionPosition(LogicalDirection.Forward);","typeGuard":"static bool IsNavigatorUsable(TextPointer p) => p.Parent is TextElement || p.GetAdjacentElement(LogicalDirection.Forward) != null;","tryCatchPattern":"try { pointer.MoveToPosition(target, dir); }\ncatch (InvalidOperationException) { pointer = document.ContentStart; }","preventionTips":["Re-acquire pointers after any document restructure","Never cache TextPointers across edits or async UI updates","Use pointer.HasValidLayout / Parent checks before moves"],"tags":["wpf","textpointer","stale-reference"],"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-21T21:30:21.729Z"}