{"record":{"id":"72067cffda27f165","repo":"AvaloniaUI/Avalonia","slug":"the-control-already-has-a-parent","errorCode":null,"errorMessage":"The Control already has a parent.","messagePattern":"The Control already has a parent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/StyledElement.cs","lineNumber":465,"sourceCode":"        {\n            value = null;\n            return (_resources?.TryGetResource(key, theme, out value) ?? false) ||\n                   (_styles?.TryGetResource(key, theme, out value) ?? false);\n        }\n\n        /// <summary>\n        /// Sets the styled element's logical parent.\n        /// </summary>\n        /// <param name=\"parent\">The parent.</param>\n        void ISetLogicalParent.SetParent(ILogical? parent)\n        {\n            var old = Parent;\n\n            if (parent != old)\n            {\n                if (old != null && parent != null)\n                {\n                    throw new InvalidOperationException(\"The Control already has a parent.\");\n                }\n\n                if (InheritanceParent == null || parent == null)\n                {\n                    InheritanceParent = parent as AvaloniaObject;\n                }\n\n                Parent = (StyledElement?)parent;\n\n                if (_logicalRoot != null)\n                {\n                    var e = new LogicalTreeAttachmentEventArgs(_logicalRoot, this, old!);\n                    OnDetachedFromLogicalTreeCore(e);\n                }\n\n                var newRoot = FindLogicalRoot(this);\n\n                if (newRoot is object)","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/StyledElement.cs#L447-L483","documentation":"Error \"The Control already has a parent.\" thrown in AvaloniaUI/Avalonia.","triggerScenarios":"Raised when a control that already has a logical parent is added to another logical tree. Defend by removing the control from its existing parent first and checking LogicalParent before re-parenting.","commonSituations":"See trigger scenarios.","solutions":["Remove the control from its current parent's child collection before adding it to a new parent.","Verify logic that moves controls between panels so the control is detached first."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}