AvaloniaUI/Avalonia · error · ArgumentException
Attempt to call InvalidateMeasure on wrong LayoutManager.
Error message
Attempt to call InvalidateMeasure on wrong LayoutManager.
What it means
InvalidateMeasure throws when a control whose layout root does not match the LayoutManager's owner is passed for invalidation. Each LayoutManager is scoped to one layout root; calling it on a control that belongs to a different visual tree root indicates a cross-tree misuse of the layout invalidation API. The guard fires after confirming the control IS attached to a visual tree, but is rooted elsewhere.
Source
Thrown at src/Avalonia.Base/Layout/LayoutManager.cs:68
if (_disposed)
{
return;
}
if (!control.IsAttachedToVisualTree)
{
#if DEBUG
throw new AvaloniaInternalException(
"LayoutManager.InvalidateMeasure called on a control that is detached from the visual tree.");
#else
return;
#endif
}
if (control.GetLayoutRoot() != _owner)
{
throw new ArgumentException("Attempt to call InvalidateMeasure on wrong LayoutManager.");
}
_toMeasure.Enqueue(control);
QueueLayoutPass();
}
/// <inheritdoc/>
public virtual void InvalidateArrange(Layoutable control)
{
control = control ?? throw new ArgumentNullException(nameof(control));
Dispatcher.UIThread.VerifyAccess();
if (_disposed)
{
return;
}
if (!control.IsAttachedToVisualTree)View on GitHub (pinned to 11c5427268)
Solutions
- Use the control's own layout root manager — call control.GetLayoutRoot().LayoutManager or let the control invalidate itself via its Layoutable.InvalidateMeasure() method.
- Ensure the control is properly removed from its old visual tree before adding it to a new one.
- Avoid caching LayoutManager references across reparenting operations.
Example fix
// before _someSharedLayoutManager.InvalidateMeasure(myControl); // after myControl.InvalidateMeasure(); // routes through the correct manager automatically
Defensive patterns
Strategy: validation
Validate before calling
// Always invalidate through the control itself rather than a cached manager
// control.InvalidateMeasure() routes to the correct manager automatically.
// If you must use a manager directly, verify ownership:
if (control.GetLayoutRoot() == layoutManagerOwner)
layoutManager.InvalidateMeasure(control); Prevention
- Prefer control.InvalidateMeasure() over direct LayoutManager calls.
- Never cache LayoutManager references across reparenting operations.
- After reparenting a control, ensure old invalidation queues no longer target it.
When it happens
Trigger: Calling layoutManager.InvalidateMeasure(control) where control.GetLayoutRoot() != layoutManager._owner. Can happen when a control is reparented between windows, or when a shared/global LayoutManager reference is used instead of the control's own layout root's manager.
Common situations: Reparenting a control between different top-level windows without going through proper removal/add. Holding a stale LayoutManager reference after the control moved. A control moved to a popup or overlay with its own layout root while a previous manager still tries to invalidate it.
Related errors
- Attempt to call InvalidateArrange on wrong LayoutManager.
- Transition elements have different parents.
- Controls for PageSlide must have same parent.
- Cannot determine visual parent.
- The control isn't currently attached to a toplevel
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/86159067bd13f317.
Report an issue: GitHub.