stride3d/stride · error · InvalidOperationException

Element is not a child of this element.

Error message

Element is not a child of this element.

What it means

PropagateCollapseToChild propagates a collapse state to a direct visual child. Stride throws InvalidOperationException when the given element's VisualParent is not 'this', because collapse propagation only makes sense along the visual tree parent-child edge.

Solutions

  1. Only pass elements whose VisualParent equals the calling element (direct visual children).
  2. Add the child to the parent's visual children before propagating collapse.
  3. Iterate to the grandchild via its direct parent instead of skipping a level.

Example fix

// before
panel.PropagateCollapseToChild(panel.Children[0].Children[0]); // grandchild
// after
foreach (var child in panel.VisualChildren)
    panel.PropagateCollapseToChild(child);
Defensive patterns

Strategy: type-guard

Validate before calling

if (child.VisualParent == this)
    PropagateCollapseToChild(child);

Type guard

bool IsDirectVisualChild(UIElement parent, UIElement child) => ReferenceEquals(child?.VisualParent, parent);

Try / catch

try { PropagateCollapseToChild(child); }
catch (InvalidOperationException) { /* child not a direct visual child; walk tree instead */ }

Prevention

When it happens

Trigger: Calling PropagateCollapseToChild with an element that is a logical child but not a visual child, a grandchild, an unrelated element, or null.

Common situations: Custom panels collapsing descendants two levels down; passing logical (content) children of a ContentControl that are not yet attached visually; refactoring that changed the tree hierarchy.

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/0741a99c811f5934. Report an issue: GitHub.

Appendix: source

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

        {
            DesiredSize = Vector3.Zero;
            DesiredSizeWithMargins = Vector3.Zero;
            RenderSize = Vector3.Zero;
            RenderOffsets = Vector3.Zero;

            foreach (var child in VisualChildrenCollection)
                PropagateCollapseToChild(child);
        }

        /// <summary>
        /// Propagate the collapsing to a child element <paramref name="element"/>.
        /// </summary>
        /// <param name="element">A child element to which propagate the collapse.</param>
        /// <exception cref="InvalidOperationException"><paramref name="element"/> is not a child of this element.</exception>
        protected void PropagateCollapseToChild(UIElement element)
        {
            if (element.VisualParent != this)
                throw new InvalidOperationException("Element is not a child of this element.");

            element.InvalidateMeasure();
            element.CollapseOverride();
        }

        /// <summary>
        /// Finds an element that has the provided identifier name in the element children.
        /// </summary>
        /// <param name="name">The name of the requested element.</param>
        /// <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);
        }

View on GitHub (pinned to 96fad776d2)