stride3d/stride · error · InvalidOperationException

The UI element 'Name=

Error message

The UI element 'Name={child.Name}' has already as parent the element 'Name={child.Parent.Name}'.

What it means

SetParent assigns the logical parent of a UIElement. Stride throws InvalidOperationException when the child already has a different logical parent, because a UI element is allowed exactly one logical parent and silently re-parenting would leave the old parent's child list inconsistent.

Solutions

  1. Remove the child from its current parent (detaching/clearing the child collection) before assigning the new parent.
  2. Pass the existing parent as the newParent argument if the intent is to re-affirm the same parent.
  3. Clone or create a new element instance instead of sharing one element between two parents.

Example fix

// before
oldPanel.Children.Remove(sharedButton); // if this does not clear Parent, guard:
UIElement.SetParent(sharedButton, newPanel);
// after
if (sharedButton.Parent == null || sharedButton.Parent == newPanel)
    UIElement.SetParent(sharedButton, newPanel);
else
    sharedButton.Parent.Children.Remove(sharedButton); // detach first
Defensive patterns

Strategy: type-guard

Validate before calling

if (child.Parent == null || ReferenceEquals(child.Parent, newParent))
    UIElement.SetParent(child, newParent);

Type guard

bool CanReparent(UIElement child, UIElement newParent) => child.Parent == null || ReferenceEquals(child.Parent, newParent);

Try / catch

try { UIElement.SetParent(child, newParent); }
catch (InvalidOperationException) { /* detach from current parent, then retry */ }

Prevention

When it happens

Trigger: Calling SetParent(child, newParent) while child.Parent is a non-null element other than newParent; adding the same element instance to two different parents.

Common situations: Reusing a single element instance (e.g. a shared Button) in multiple pages or panels; moving an element between containers without removing it from the first; accidental sharing of template content across instances.

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 stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/bd7bd3d0ffd9d48f. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.UI/UIElement.cs:1120

        /// <returns>The requested element. This can be null if no matching element was found.</returns>
        /// <remarks>If several elements with the same name exist return the first found</remarks>
        public UIElement FindName(string name)
        {
            if (Name == name)
                return this;

            return VisualChildren.Select(child => child.FindName(name)).FirstOrDefault(elt => elt != null);
        }

        /// <summary>
        /// Set the parent to a child.
        /// </summary>
        /// <param name="child">The child to which set the parent.</param>
        /// <param name="parent">The parent of the child.</param>
        protected static void SetParent([NotNull] UIElement child, [CanBeNull] UIElement parent)
        {
            if (parent != null && child.Parent != null && parent != child.Parent)
                throw new InvalidOperationException("The UI element 'Name="+child.Name+"' has already as parent the element 'Name="+child.Parent.Name+"'.");

            child.Parent = parent;
        }

        /// <summary>
        /// Set the visual parent to a child.
        /// </summary>
        /// <param name="child">The child to which set the visual parent.</param>
        /// <param name="parent">The parent of the child.</param>
        protected static void SetVisualParent([NotNull] UIElement child, [CanBeNull] UIElement parent)
        {
            if (child == null) throw new ArgumentNullException(nameof(child));
            if (parent != null && child.VisualParent != null && parent != child.VisualParent)
                throw new InvalidOperationException("The UI element 'Name=" + child.Name + "' has already as visual parent the element 'Name=" + child.VisualParent.Name + "'.");

            child.VisualParent?.VisualChildrenCollection.Remove(child);

            child.VisualParent = parent;

View on GitHub (pinned to 96fad776d2)