dotnet/wpf · error · InvalidOperationException
SR.TextViewInvalidLayout
Error message
SR.TextViewInvalidLayout
What it means
TextDocumentView.GetTextPositionFromPoint requires a valid layout. If IsValid is false (layout is out of date or not computed), it throws InvalidOperationException(SR.TextViewInvalidLayout) because hit-testing against stale or missing layout would return wrong positions.
Solutions
- Ensure a layout pass has occurred (call UpdateLayout or wait for Loaded/rendered) before hit-testing.
- Check the view's IsValid property first and skip/retry when false.
- Subscribe to layout-updated/activated events from the text view and only query when valid.
- Call from the UI thread after the editor reports layout validity (e.g. TextView.LayoutUpdated).
Example fix
// before
var pos = textView.GetTextPositionFromPoint(point, true);
// after
if (textView.IsValid)
var pos = textView.GetTextPositionFromPoint(point, true); Defensive patterns
Strategy: type-guard
Validate before calling
if (!textView.IsValid) return null;
Type guard
static bool CanHitTest(ITextView view) => view is { IsValid: true }; Try / catch
try { return view.GetTextPositionFromPoint(point, snapToText); }
catch (InvalidOperationException) { return null; // layout not ready } Prevention
- Only query the view after a completed layout pass (UpdateLayout / Loaded).
- Check IsValid before every view query.
- Subscribe to the view's layout-updated event instead of polling after edits.
- Avoid hit-testing in constructors or before the control is rendered.
When it happens
Trigger: Calling GetTextPositionFromPoint after the document content changed but before layout was updated, or after the view was invalidated/disposed (e.g. document unloaded, control not yet rendered).
Common situations: Hit-testing a TextEditor/TextBox view during or immediately after editing, before a layout pass; querying positions in a window not yet shown; automation code running on the UI thread before measure/arrange.
Related errors
- SR.TextViewInvalidLayout
- SR.TextViewInvalidLayout
- 0x80040206
- Argument out of range (position not contained in view)
- ArgumentNullException: child
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b80cc5d99ed4614e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/TextDocumentView.cs:64
#endregion Constructors
//-------------------------------------------------------------------
//
// Internal Methods
//
//-------------------------------------------------------------------
#region Internal Methods
/// <summary>
/// <see cref="ITextView.GetTextPositionFromPoint"/>
/// </summary>
internal override ITextPointer GetTextPositionFromPoint(Point point, bool snapToText)
{
// Verify that layout information is valid. Cannot continue if not valid.
if (!IsValid)
{
throw new InvalidOperationException(SR.TextViewInvalidLayout);
}
_owner.EnsureValidVisuals();
// Transforms point to content's coordinate system.
TransformToContent(ref point);
// Search columns
return GetTextPositionFromPoint(Columns, FloatingElements, point, snapToText);
}
/// <summary>
/// <see cref="ITextView.GetRawRectangleFromTextPosition"/>
/// </summary>
/// <remarks>
/// TextDocumentView does not calculate any transform for this function. Transform returned is always identity.
/// </remarks>
internal override Rect GetRawRectangleFromTextPosition(ITextPointer position, out Transform transform)View on GitHub (pinned to 81131a70a4)