{"record":{"id":"0741a99c811f5934","repo":"stride3d/stride","slug":"element-is-not-a-child-of-this-element","errorCode":null,"errorMessage":"Element is not a child of this element.","messagePattern":"Element is not a child of this element\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.UI/UIElement.cs","lineNumber":1092,"sourceCode":"        {\n            DesiredSize = Vector3.Zero;\n            DesiredSizeWithMargins = Vector3.Zero;\n            RenderSize = Vector3.Zero;\n            RenderOffsets = Vector3.Zero;\n\n            foreach (var child in VisualChildrenCollection)\n                PropagateCollapseToChild(child);\n        }\n\n        /// <summary>\n        /// Propagate the collapsing to a child element <paramref name=\"element\"/>.\n        /// </summary>\n        /// <param name=\"element\">A child element to which propagate the collapse.</param>\n        /// <exception cref=\"InvalidOperationException\"><paramref name=\"element\"/> is not a child of this element.</exception>\n        protected void PropagateCollapseToChild(UIElement element)\n        {\n            if (element.VisualParent != this)\n                throw new InvalidOperationException(\"Element is not a child of this element.\");\n\n            element.InvalidateMeasure();\n            element.CollapseOverride();\n        }\n\n        /// <summary>\n        /// Finds an element that has the provided identifier name in the element children.\n        /// </summary>\n        /// <param name=\"name\">The name of the requested element.</param>\n        /// <returns>The requested element. This can be null if no matching element was found.</returns>\n        /// <remarks>If several elements with the same name exist return the first found</remarks>\n        public UIElement FindName(string name)\n        {\n            if (Name == name)\n                return this;\n\n            return VisualChildren.Select(child => child.FindName(name)).FirstOrDefault(elt => elt != null);\n        }","sourceCodeStart":1074,"sourceCodeEnd":1110,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.UI/UIElement.cs#L1074-L1110","documentation":"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.","triggerScenarios":"Calling PropagateCollapseToChild with an element that is a logical child but not a visual child, a grandchild, an unrelated element, or null.","commonSituations":"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.","solutions":["Only pass elements whose VisualParent equals the calling element (direct visual children).","Add the child to the parent's visual children before propagating collapse.","Iterate to the grandchild via its direct parent instead of skipping a level."],"exampleFix":"// before\npanel.PropagateCollapseToChild(panel.Children[0].Children[0]); // grandchild\n// after\nforeach (var child in panel.VisualChildren)\n    panel.PropagateCollapseToChild(child);","handlingStrategy":"type-guard","validationCode":"if (child.VisualParent == this)\n    PropagateCollapseToChild(child);","typeGuard":"bool IsDirectVisualChild(UIElement parent, UIElement child) => ReferenceEquals(child?.VisualParent, parent);","tryCatchPattern":"try { PropagateCollapseToChild(child); }\ncatch (InvalidOperationException) { /* child not a direct visual child; walk tree instead */ }","preventionTips":["Only iterate direct VisualChildren when propagating collapse","Ensure children are visually attached before collapse propagation","For deeper nodes, recurse through each intermediate parent"],"tags":["invalid-state","ui","visual-tree","hierarchy"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}