{"record":{"id":"3a98b96e04c8a0e2","repo":"stride3d/stride","slug":"the-ui-element-name-child-name-has-already-as-visual-parent","errorCode":null,"errorMessage":"The UI element 'Name={child.Name}' has already as visual parent the element 'Name={child.VisualParent.Name}'.","messagePattern":"The UI element 'Name=(.+?)' has already as visual parent the element 'Name=(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.UI/UIElement.cs","lineNumber":1134,"sourceCode":"        /// <param name=\"parent\">The parent of the child.</param>\n        protected static void SetParent([NotNull] UIElement child, [CanBeNull] UIElement parent)\n        {\n            if (parent != null && child.Parent != null && parent != child.Parent)\n                throw new InvalidOperationException(\"The UI element 'Name=\"+child.Name+\"' has already as parent the element 'Name=\"+child.Parent.Name+\"'.\");\n\n            child.Parent = parent;\n        }\n\n        /// <summary>\n        /// Set the visual parent to a child.\n        /// </summary>\n        /// <param name=\"child\">The child to which set the visual parent.</param>\n        /// <param name=\"parent\">The parent of the child.</param>\n        protected static void SetVisualParent([NotNull] UIElement child, [CanBeNull] UIElement parent)\n        {\n            if (child == null) throw new ArgumentNullException(nameof(child));\n            if (parent != null && child.VisualParent != null && parent != child.VisualParent)\n                throw new InvalidOperationException(\"The UI element 'Name=\" + child.Name + \"' has already as visual parent the element 'Name=\" + child.VisualParent.Name + \"'.\");\n\n            child.VisualParent?.VisualChildrenCollection.Remove(child);\n\n            child.VisualParent = parent;\n\n            if (parent != null)\n            {\n                child.LayoutingContext = parent.layoutingContext;\n                parent.VisualChildrenCollection.Add(child);\n            }\n        }\n\n        /// <summary>\n        /// Calculate the intersection of the UI element and the ray.\n        /// </summary>\n        /// <param name=\"ray\">The ray in world space coordinate</param>\n        /// <param name=\"intersectionPoint\">The intersection point in world space coordinate</param>\n        /// <returns><value>true</value> if the two elements intersects, <value>false</value> otherwise</returns>","sourceCodeStart":1116,"sourceCodeEnd":1152,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.UI/UIElement.cs#L1116-L1152","documentation":"SetVisualParent assigns the visual (rendering tree) parent of a UIElement and throws ArgumentNullException for a null child, and InvalidOperationException when the child already has a different visual parent. A visual element may have exactly one visual parent so the render tree stays a tree.","triggerScenarios":"Calling SetVisualParent(null, parent); re-parenting a child whose VisualParent is set to a different element; adding an element instance to two visual containers.","commonSituations":"Shared visual elements across scenes or overlays; drag-and-drop re-parenting without detaching first; z-order/overlay code that reuses one visual node.","solutions":["Pass a non-null child element.","Detach the child from its current visual parent (which removes it from the old parent's VisualChildrenCollection) before assigning a new one.","Verify via child.VisualParent == null (or == newParent) before calling SetVisualParent."],"exampleFix":"// before\nUIElement.SetVisualParent(child, newParent); // child still attached elsewhere\n// after\nif (child.VisualParent != null && child.VisualParent != newParent)\n    UIElement.SetVisualParent(child, null); // detach\nUIElement.SetVisualParent(child, newParent);","handlingStrategy":"type-guard","validationCode":"if (child != null && (child.VisualParent == null || ReferenceEquals(child.VisualParent, newParent)))\n    UIElement.SetVisualParent(child, newParent);","typeGuard":"bool CanSetVisualParent(UIElement child, UIElement newParent) => child != null && (child.VisualParent == null || ReferenceEquals(child.VisualParent, newParent));","tryCatchPattern":"try { UIElement.SetVisualParent(child, newParent); }\ncatch (InvalidOperationException) { UIElement.SetVisualParent(child, null); UIElement.SetVisualParent(child, newParent); }","preventionTips":["Detach (SetVisualParent(child, null)) before attaching elsewhere","Check VisualParent before drag-and-drop or overlay re-parenting","Assert child != null before calling any parent setter"],"tags":["invalid-state","ui","visual-tree","null-argument"],"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"}