dotnet/wpf · error · InvalidOperationException

SR.TextViewInvalidLayout

Error message

SR.TextViewInvalidLayout

What it means

TextViewBase.GetGlyphRuns throws InvalidOperationException(SR.TextViewInvalidLayout) when IsValid is false. Glyph run enumeration is only meaningful against current rendered geometry; the base view refuses to return stale (or empty) results when layout has been invalidated. Subclasses such as TextParagraphView inherit the same guard. Refresh layout before enumerating glyph runs.

Solutions

  1. Check textView.IsValid and call Validate() prior to GetGlyphRuns.
  2. Call UpdateLayout() on the host control so the view rebuilds its layout.
  3. Hook TextView.Updated and enumerate glyph runs only while IsValid is true.
  4. Catch InvalidOperationException, await re-layout (Dispatcher at Loaded priority), then retry.

Example fix

// before
var runs = textView.GetGlyphRuns(start, end);
// after
if (!textView.IsValid) textView.Validate();
var runs = textView.IsValid ? textView.GetGlyphRuns(start, end) : null;
Defensive patterns

Strategy: validation

Validate before calling

if (!textView.IsValid) textView.Validate();
if (!textView.IsValid) return Array.Empty<GlyphRun>();

Type guard

static bool ReadyForGlyphEnumeration(ITextView view) => view != null && view.IsValid;

Try / catch

try { runs = textView.GetGlyphRuns(start, end); }
catch (InvalidOperationException) { runs = null; /* wait for Updated */ }

Prevention

When it happens

Trigger: Calling GetGlyphRuns (used by rendering/spell-check underline drawing) after layout invalidation — text edit, font/theme change, resize — or before the first layout pass completed.

Common situations: Custom adorners drawing squiggles while the editor is re-flowing; screen-reader or automation code enumerating glyphs on a not-yet-rendered control; rendering hooks firing during layout transitions.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextViewBase.cs:109

        /// <summary>
        /// <see cref="ITextView.GetBackspaceCaretUnitPosition"/>
        /// </summary>
        internal abstract ITextPointer GetBackspaceCaretUnitPosition(ITextPointer position);

        /// <summary>
        /// <see cref="ITextView.GetLineRange"/>
        /// </summary>
        internal abstract TextSegment GetLineRange(ITextPointer position);

        /// <summary>
        /// <see cref="ITextView.GetGlyphRuns"/>
        /// </summary>
        internal virtual ReadOnlyCollection<GlyphRun> GetGlyphRuns(ITextPointer start, ITextPointer end)
        {
            // Verify that layout information is valid. Cannot continue if not valid.
            if (!IsValid)
            {
                throw new InvalidOperationException(SR.TextViewInvalidLayout);
            }
            return ReadOnlyCollection<GlyphRun>.Empty;
        }

        /// <summary>
        /// <see cref="ITextView.Contains"/>
        /// </summary>
        internal abstract bool Contains(ITextPointer position);

        /// <summary>
        /// Scroll the given rectangle the minimum amount required to bring it entirely into view.
        /// </summary>
        /// <param name="textView">TextView doing the scrolling</param>
        /// <param name="rect">Rect to scroll</param>
        /// <remarks>
        /// # RECT POSITION       RECT SIZE        SCROLL      REMEDY
        /// 1 Above viewport      lte viewport     Down        Align top edge of rect and viewport
        /// 2 Above viewport      gt viewport      Down        Align bottom edge of rect and viewport

View on GitHub (pinned to 81131a70a4)