dotnet/wpf · error · InvalidOperationException

SR.NoScopingElement (This TextPointer)

Error message

SR.NoScopingElement (This TextPointer)

What it means

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").

Solutions

  1. Verify pointer.Parent is TextElement before calling GetValue; if not, move the pointer into text content (e.g. MoveToPosition within a Run) first.
  2. Get the pointer from a real content position, such as Document.ContentStart/ContentEnd after ensuring the document has a Paragraph/Run.
  3. Catch InvalidOperationException and fall back to reading formatting from the containing RichTextBox selection or defaults.

Example fix

// before
var size = (double)textPointer.GetValue(TextElement.FontSizeProperty);
// after
if (textPointer.Parent is TextElement te)
    var size = (double)te.GetValue(TextElement.FontSizeProperty);
else
    var size = (double)(richTextBox.Document.Blocks.FirstBlock as TextElement)?.GetValue(TextElement.FontSizeProperty) ?? 12.0;
Defensive patterns

Strategy: validation

Validate before calling

if (!(pointer.Parent is TextElement))
    return null; // no scoping element; skip or reposition

Type guard

static bool HasScopingElement(TextPointer p) => p.Parent is TextElement;

Try / catch

try { var v = pointer.GetValue(prop); }
catch (InvalidOperationException) { var v = defaultValue; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/434f17f3eb51ae98. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs:2532

            }

            return val;
        }

        object ITextPointer.ReadLocalValue(DependencyProperty formattingProperty)
        {
            TextElement element;

            ArgumentNullException.ThrowIfNull(formattingProperty);

            _tree.EmptyDeadPositionList();

            SyncToTreeGeneration();

            element = this.Parent as TextElement;
            if (element == null)
            {
                throw new InvalidOperationException(SR.Format(SR.NoScopingElement, "This TextPointer"));
            }

            return element.ReadLocalValue(formattingProperty);
        }

        LocalValueEnumerator ITextPointer.GetLocalValueEnumerator()
        {
            DependencyObject element;

            _tree.EmptyDeadPositionList();

            SyncToTreeGeneration();

            element = this.Parent as TextElement;
            if (element == null)
            {
                //  Look into adding an empty ctor to LocalValueEnumerator.
                return (new DependencyObject()).GetLocalValueEnumerator();

View on GitHub (pinned to 81131a70a4)