{"record":{"id":"73fb7d5704f55fc9","repo":"dotnet/wpf","slug":"sr-haslogicalparent","errorCode":null,"errorMessage":"SR.HasLogicalParent","messagePattern":"SR\\.HasLogicalParent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs","lineNumber":300,"sourceCode":"            ///////////////////\n\n            //\n            // -- Approved By The Core Team --\n            //\n            // Do not allow foreign threads to change the tree.\n            // (This is a noop if this object is not assigned to a Dispatcher.)\n            //\n            // We also need to ensure that the tree is homogenous with respect\n            // to the dispatchers that the elements belong to.\n            //\n            this.VerifyAccess();\n            newParent?.VerifyAccess();\n\n            // Logical Parent must first be dropped before you are attached to a newParent\n            // This mitigates illegal tree state caused by logical child stealing as illustrated in bug 970706\n            if (_parent != null && newParent != null && _parent != newParent)\n            {\n                throw new System.InvalidOperationException(SR.HasLogicalParent);\n            }\n\n            // Trivial check to avoid loops\n            if (newParent == this)\n            {\n                throw new System.InvalidOperationException(SR.CannotBeSelfParent);\n            }\n\n            // invalid during a VisualTreeChanged event\n            VisualDiagnostics.VerifyVisualTreeChange(this);\n\n            // Logical Parent implies no InheritanceContext\n            if (newParent != null)\n            {\n                ClearInheritanceContext();\n            }\n\n            IsParentAnFE = newParent is FrameworkElement;","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkContentElement.cs#L282-L318","documentation":"ChangeLogicalParent enforces that a logical child has exactly one parent: the existing _parent must be dropped (set to null) or be the same as newParent before re-attaching. Otherwise InvalidOperationException (HasLogicalParent) is thrown, preventing 'child stealing' and illegal tree states (see WPF bug 970706).","triggerScenarios":"Assigning an element that already has a logical parent to a second parent — e.g. adding the same UIElement to two panels, reusing an element instance in two ContentControls, or reparenting without detaching first.","commonSituations":"Reusing control instances across pages/panels; moving an element between containers without removing it first; data-binding mistakes that reuse the same visual as multiple items' content.","solutions":["Remove the child from its current logical parent before adding it to the new one.","Use separate element instances per location instead of sharing one instance.","If reparenting intentionally, set (oldParent).RemoveLogicalChild(child) first, then attach."],"exampleFix":"// before\npanelB.Children.Add(sharedElement); // still parented to panelA\n// after\npanelA.Children.Remove(sharedElement);\npanelB.Children.Add(sharedElement);","handlingStrategy":"validation","validationCode":"if (LogicalTreeHelper.GetParent(child) != null && LogicalTreeHelper.GetParent(child) != newParent)\n    ((FrameworkElement)LogicalTreeHelper.GetParent(child)).RemoveLogicalChild(child); // detach first","typeGuard":"static bool IsOrphan(DependencyObject child) => new FrameworkObject(child).Parent == null;","tryCatchPattern":"try { newParent.AddLogicalChild(child); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"parent\")) { /* detach from old parent then retry */ }","preventionTips":["Always remove an element from its current parent before adding it elsewhere.","Never share one element instance among multiple containers; create instances per location.","Check FrameworkObject.Parent (or Parent property) before reparenting."],"tags":["wpf","logical-tree","parenting","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}