AvaloniaUI/Avalonia · error · ArgumentException

Controls for PageSlide must have same parent.

Error message

Controls for PageSlide must have same parent.

What it means

Thrown by PageSlide.GetVisualParent when both 'from' and 'to' controls have non-null VisualParent values that differ. PageSlide animates two siblings sharing a common parent, so a mismatched parent makes the slide geometrically ambiguous.

Source

Thrown at src/Avalonia.Base/Animation/PageSlide.cs:233

        /// Gets the common visual parent of the two control.
        /// </summary>
        /// <param name="from">The from control.</param>
        /// <param name="to">The to control.</param>
        /// <returns>The common parent.</returns>
        /// <exception cref="ArgumentException">
        /// The two controls do not share a common parent.
        /// </exception>
        /// <remarks>
        /// Any one of the parameters may be null, but not both.
        /// </remarks>
        protected static Visual GetVisualParent(Visual? from, Visual? to)
        {
            var p1 = (from ?? to)!.VisualParent;
            var p2 = (to ?? from)!.VisualParent;

            if (p1 != null && p2 != null && p1 != p2)
            {
                throw new ArgumentException("Controls for PageSlide must have same parent.");
            }

            return p1 ?? throw new InvalidOperationException("Cannot determine visual parent.");
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Ensure both controls share the same immediate VisualParent before invoking the transition.
  2. Pass null for the control that has already been detached (the method allows one null).
  3. Use a different transition type (e.g. PageTurn) for cross-container animations.

Example fix

// before
slide.Start(from: controlA, to: controlB); // different parents

// after
// ensure both are children of the same panel first
panel.Children.Add(controlB);
slide.Start(from: controlA, to: controlB);
Defensive patterns

Strategy: validation

Validate before calling

static bool ShareParent(Visual? a, Visual? b)
{
    var pa = (a ?? b)?.VisualParent;
    var pb = (b ?? a)?.VisualParent;
    return pa is null || pb is null || pa == pb;
}

Try / catch

if (!ShareParent(from, to)) throw new InvalidOperationException("Re-parent controls first");
slide.Start(from, to);

Prevention

When it happens

Trigger: Calling a PageSlide transition between two controls that live in different panels/windows; one control reparented before the slide starts; animating controls from two different ContentControls.

Common situations: Swapping views in a ContentControl where the old content was already removed from the tree (parent becomes null on one side but the comparison still fails); using PageSlide on controls in separate TabControl tabs.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/978a34591eeb3f58. Report an issue: GitHub.