{"record":{"id":"434f17f3eb51ae98","repo":"dotnet/wpf","slug":"sr-noscopingelement-this-textpointer","errorCode":null,"errorMessage":"SR.NoScopingElement (This TextPointer)","messagePattern":"SR\\.NoScopingElement \\(This TextPointer\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":2532,"sourceCode":"            }\n\n            return val;\n        }\n\n        object ITextPointer.ReadLocalValue(DependencyProperty formattingProperty)\n        {\n            TextElement element;\n\n            ArgumentNullException.ThrowIfNull(formattingProperty);\n\n            _tree.EmptyDeadPositionList();\n\n            SyncToTreeGeneration();\n\n            element = this.Parent as TextElement;\n            if (element == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.NoScopingElement, \"This TextPointer\"));\n            }\n\n            return element.ReadLocalValue(formattingProperty);\n        }\n\n        LocalValueEnumerator ITextPointer.GetLocalValueEnumerator()\n        {\n            DependencyObject element;\n\n            _tree.EmptyDeadPositionList();\n\n            SyncToTreeGeneration();\n\n            element = this.Parent as TextElement;\n            if (element == null)\n            {\n                //  Look into adding an empty ctor to LocalValueEnumerator.\n                return (new DependencyObject()).GetLocalValueEnumerator();","sourceCodeStart":2514,"sourceCodeEnd":2550,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L2514-L2550","documentation":"The ITextPointer/TextPointer GetValue method on formatting properties requires the pointer to be parented by a TextElement (Run, Paragraph, etc.), which acts as the scoping element for the property value. When this.Parent is not a TextElement (or is null), there is no scoping element, so it throws InvalidOperationException(SR.NoScopingElement, \"This TextPointer\").","triggerScenarios":"Calling TextPointer.GetValue(TextElement.FontSizeProperty) (or any formatting property) on a pointer whose Parent is not a TextElement — e.g. a pointer at the document root, at a position between elements, or on a detached/empty TextContainer.","commonSituations":"Querying formatting at a TextPointer obtained from a fresh empty RichTextBox (no paragraphs yet); using a pointer positioned at the root before any content is typed; pointers invalidated after document edits.","solutions":["Verify pointer.Parent is TextElement before calling GetValue; if not, move the pointer into text content (e.g. MoveToPosition within a Run) first.","Get the pointer from a real content position, such as Document.ContentStart/ContentEnd after ensuring the document has a Paragraph/Run.","Catch InvalidOperationException and fall back to reading formatting from the containing RichTextBox selection or defaults."],"exampleFix":"// before\nvar size = (double)textPointer.GetValue(TextElement.FontSizeProperty);\n// after\nif (textPointer.Parent is TextElement te)\n    var size = (double)te.GetValue(TextElement.FontSizeProperty);\nelse\n    var size = (double)(richTextBox.Document.Blocks.FirstBlock as TextElement)?.GetValue(TextElement.FontSizeProperty) ?? 12.0;","handlingStrategy":"validation","validationCode":"if (!(pointer.Parent is TextElement))\n    return null; // no scoping element; skip or reposition","typeGuard":"static bool HasScopingElement(TextPointer p) => p.Parent is TextElement;","tryCatchPattern":"try { var v = pointer.GetValue(prop); }\ncatch (InvalidOperationException) { var v = defaultValue; }","preventionTips":["Verify pointer.Parent is TextElement before reading formatting","Only read formatting from pointers inside Run/Paragraph content","Handle empty-document pointers explicitly"],"tags":["wpf","textpointer","formatting"],"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"}