{"record":{"id":"3947078da9ec318b","repo":"dotnet/wpf","slug":"sr-haslogicalparent-frameworkelement","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/FrameworkElement.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/FrameworkElement.cs#L282-L318","documentation":"ChangeLogicalParent (internal logical-tree plumbing used by FrameworkElement.AddLogicalChild/parent assignment) enforces that a child must be detached from its current logical parent before being attached to a new one. If _parent is non-null and differs from newParent, WPF throws InvalidOperationException (HasLogicalParent) to prevent 'child stealing' and corrupt trees.","triggerScenarios":"Adding an element that already has a logical parent to a second parent: calling newParent.AddLogicalChild(child) where child.Parent (or logical parent) is another element; reparenting without removing from the old parent first; sharing one child instance between two containers.","commonSituations":"Reusing a single UI element instance in multiple Panels/Tabs; moving controls between containers without detaching; content assigned to two ContentControls simultaneously.","solutions":["Remove the child from its current parent first (oldParent.RemoveLogicalChild(child) or remove from the panel's Children) before adding it to the new parent.","Use XamlReader/clone or construct a new element instance if the element must appear in two places.","For ContentControls, reassign the old holder's Content to null before setting it on the new holder."],"exampleFix":"// before\npanelB.Children.Add(sharedButton); // InvalidOperationException: has logical parent panelA\n\n// after\npanelA.Children.Remove(sharedButton);\npanelB.Children.Add(sharedButton);","handlingStrategy":"validation","validationCode":"var logicalParent = LogicalTreeHelper.GetParent(child);\nif (logicalParent != null && logicalParent != newParent)\n    throw new InvalidOperationException(\"Child already has a logical parent; detach first.\");","typeGuard":"bool IsOrphan(FrameworkElement child) => LogicalTreeHelper.GetParent(child) == null;","tryCatchPattern":"try\n{\n    newParent.AddLogicalChild(child);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"parent\"))\n{\n    Log.Warn(\"Child already attached to another logical parent.\", ex);\n}","preventionTips":["Always remove an element from its current parent before adding it elsewhere.","Never share one element instance between two containers; clone or create new instances instead.","Null out Content/Child on the old holder before reassigning to a new holder."],"tags":["wpf","logical-tree","parent","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-21T21:30:21.729Z"}