dotnet/wpf · error · InvalidOperationException

SR.TextViewInvalidLayout

Error message

SR.TextViewInvalidLayout

What it means

MultiPageTextView.GetTextPositionFromPoint requires valid layout before mapping a screen point to a text position. When the IsValid property is false the view has no up-to-date layout and cannot answer geometry queries, so it throws InvalidOperationException with SR.TextViewInvalidLayout.

Solutions

  1. Wait for layout to complete before calling — invoke from LayoutUpdated or after the DocumentPageView reports valid pages.
  2. Check MultiPageTextView.IsValid before the call and skip or defer the operation when false.
  3. Subscribe to the TextView's Updated/LayoutUpdated event and perform the query there.
  4. Force a layout update (UpdateLayout) on the containing viewer prior to the call.

Example fix

// before
var position = textView.GetTextPositionFromPoint(point, snapToText); // throws if layout invalid

// after
if (textView.IsValid)
{
    var position = textView.GetTextPositionFromPoint(point, snapToText);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!textView.IsValid)
{
    return null; // or defer until TextView.Updated fires
}
var position = textView.GetTextPositionFromPoint(point, snapToText);

Try / catch

try
{
    position = textView.GetTextPositionFromPoint(point, snapToText);
}
catch (InvalidOperationException)
{
    // layout invalid — defer via Dispatcher or Updated event
}

Prevention

When it happens

Trigger: Calling GetTextPositionFromPoint (directly or via BringPointIntoViewAsync, GetPositionAtNextLineCore, GetPositionAtNextPageCore) while MultiPageTextView.IsValid is false — i.e. before layout completes, after layout was invalidated, or while the DocumentPageView has no valid pages.

Common situations: Querying the text view from mouse-hit-testing code before the first layout pass finished; calling text-view APIs inside a layout-invalidating handler; pages not yet generated in a paginated DocumentPageView (e.g. before the viewer is rendered).

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/52c38038c53e80f8. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/MultiPageTextView.cs:97

        //
        //  Internal Methods
        //
        //-------------------------------------------------------------------

        #region Internal Methods

        /// <summary>
        /// <see cref="ITextView.GetTextPositionFromPoint"/>
        /// </summary>
        internal override ITextPointer GetTextPositionFromPoint(Point point, bool snapToText)
        {
            ITextPointer position = null;
            DocumentPageTextView pageTextView;

            // Verify that layout information is valid. Cannot continue if not valid.
            if (!IsValid)
            {
                throw new InvalidOperationException(SR.TextViewInvalidLayout);
            }

            pageTextView = GetTextViewFromPoint(point, false);
            if (pageTextView != null)
            {
                // Transform to DocumentPageView coordinates and query inner TextView
                point = TransformToDescendant(pageTextView.RenderScope, point);
                position = pageTextView.GetTextPositionFromPoint(point, snapToText);
            }
            return position;
        }

        /// <summary>
        /// <see cref="ITextView.GetRawRectangleFromTextPosition"/>
        /// </summary>
        internal override Rect GetRawRectangleFromTextPosition(ITextPointer position, out Transform transform)
        {
            Rect rect = Rect.Empty;

View on GitHub (pinned to 81131a70a4)