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 TransitioningContentControlPageViewModel.GetVisualParent (ArgumentException) when the two controls' visual parents both exist but are different objects. A PageSlide transition needs both the outgoing and incoming content under the same parent to animate a slide.

Solutions

  1. Host both 'from' and 'to' content in the same parent container.
  2. Ensure the TransitioningContentControl owns both visuals before the transition runs.
  3. Pass a single non-null argument (the other null) when only one side is available, which skips the parent-mismatch check.

Example fix

// before
var parent = GetVisualParent(from, to); // different parents -> throws

// after
// ensure both share TransitioningContentControl's content host
sameParent.Children.Add(to);
var parent = GetVisualParent(from, to);
Defensive patterns

Strategy: validation

Validate before calling

var p1 = (from ?? to)?.GetVisualParent();
var p2 = (to ?? from)?.GetVisualParent();
if (p1 != null && p2 != null && p1 != p2) { /* host both under the same parent */ }

Type guard

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

Try / catch

try { var p = GetVisualParent(from, to); }
catch (ArgumentException ex) when (ex.Message.Contains("same parent")) { /* reparent and retry */ }

Prevention

When it happens

Trigger: Calling GetVisualParent(from, to) where p1 = from's parent and p2 = to's parent are both non-null and not equal. The controls are hosted in different containers at transition time.

Common situations: TransitioningContentControl misconfigured with content in separate panels; reparenting during a page change; custom transition hosting that splits the two visuals across panels.

Related errors


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

Appendix: source

Thrown at samples/ControlCatalog/ViewModels/TransitioningContentControlPageViewModel.cs:311

        /// 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>
        private static Visual GetVisualParent(Visual? from, Visual? to)
        {
            var p1 = (from ?? to)!.GetVisualParent();
            var p2 = (to ?? from)!.GetVisualParent();

            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)