{"record":{"id":"5b92ea266244e052","repo":"dotnet/wpf","slug":"sr-cannotbeselfparent-frameworkelement","errorCode":null,"errorMessage":"SR.CannotBeSelfParent","messagePattern":"SR\\.CannotBeSelfParent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs","lineNumber":306,"sourceCode":"            // (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;\n\n            DependencyObject oldParent = _parent;\n            OnNewParent(newParent);\n\n            // Update Has[Loaded/Unloaded]Handler Flags\n            BroadcastEventHelper.AddOrRemoveHasLoadedChangeHandlerFlag(this, oldParent, newParent);","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs#L288-L324","documentation":"ChangeLogicalParent performs a trivial loop check: newParent == this would make the element its own logical parent, producing a cycle in the logical tree. WPF throws InvalidOperationException (CannotBeSelfParent) to keep the tree acyclic.","triggerScenarios":"Calling element.AddLogicalChild(element) (or internal ChangeLogicalParent with newParent == the child itself) — attempting to make an element the logical child of itself.","commonSituations":"Recursive builder code where the 'child' variable accidentally aliases the parent (e.g. assigning a container to its own Content/Child property); off-by-one logic in programmatic tree construction.","solutions":["Add a guard: if (child != container) before calling AddLogicalChild / assigning Content.","Review the code that builds the hierarchy to ensure the child reference is a distinct element instance.","Assert the invariant in debug builds: Debug.Assert(parent != child)."],"exampleFix":"// before\ncontainer.Content = container; // InvalidOperationException: cannot be self-parent\n\n// after\nif (child != container)\n{\n    container.Content = child;\n}","handlingStrategy":"validation","validationCode":"if (ReferenceEquals(child, parent))\n    throw new InvalidOperationException(\"An element cannot be its own logical parent.\");","typeGuard":"bool CanParent(FrameworkElement parent, FrameworkElement child) => !ReferenceEquals(parent, child);","tryCatchPattern":"try\n{\n    parent.AddLogicalChild(child);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"parent\"))\n{\n    Log.Warn(\"Attempted to attach an element to itself.\", ex);\n}","preventionTips":["Guard against reference aliasing when building trees programmatically (parent != child).","Add Debug.Assert(!ReferenceEquals(container, content)) in builder helpers."],"tags":["wpf","logical-tree","cyclic-reference","invalid-operation"],"backgroundTag":"cyclic-style-reference-detected","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"}