{"record":{"id":"bd7bd3d0ffd9d48f","repo":"stride3d/stride","slug":"the-ui-element-name-child-name-has-already-as-parent-the","errorCode":null,"errorMessage":"The UI element 'Name={child.Name}' has already as parent the element 'Name={child.Parent.Name}'.","messagePattern":"The UI element 'Name=(.+?)' has already as parent the element 'Name=(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.UI/UIElement.cs","lineNumber":1120,"sourceCode":"        /// <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        }\n\n        /// <summary>\n        /// Set the parent to a child.\n        /// </summary>\n        /// <param name=\"child\">The child to which set the parent.</param>\n        /// <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;","sourceCodeStart":1102,"sourceCodeEnd":1138,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.UI/UIElement.cs#L1102-L1138","documentation":"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.","triggerScenarios":"Calling SetParent(child, newParent) while child.Parent is a non-null element other than newParent; adding the same element instance to two different parents.","commonSituations":"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.","solutions":["Remove the child from its current parent (detaching/clearing the child collection) before assigning the new parent.","Pass the existing parent as the newParent argument if the intent is to re-affirm the same parent.","Clone or create a new element instance instead of sharing one element between two parents."],"exampleFix":"// before\noldPanel.Children.Remove(sharedButton); // if this does not clear Parent, guard:\nUIElement.SetParent(sharedButton, newPanel);\n// after\nif (sharedButton.Parent == null || sharedButton.Parent == newPanel)\n    UIElement.SetParent(sharedButton, newPanel);\nelse\n    sharedButton.Parent.Children.Remove(sharedButton); // detach first","handlingStrategy":"type-guard","validationCode":"if (child.Parent == null || ReferenceEquals(child.Parent, newParent))\n    UIElement.SetParent(child, newParent);","typeGuard":"bool CanReparent(UIElement child, UIElement newParent) => child.Parent == null || ReferenceEquals(child.Parent, newParent);","tryCatchPattern":"try { UIElement.SetParent(child, newParent); }\ncatch (InvalidOperationException) { /* detach from current parent, then retry */ }","preventionTips":["Never share one element instance between two containers","Remove the element from its current parent before re-parenting","Clone shared template content per usage site"],"tags":["invalid-state","ui","parent","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"}