{"record":{"id":"5cd1a21bfb617f03","repo":"dotnet/wpf","slug":"sr-cannotbeselfparent-frameworkelementtemplate","errorCode":null,"errorMessage":"SR.CannotBeSelfParent","messagePattern":"SR\\.CannotBeSelfParent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs","lineNumber":370,"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":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs#L352-L388","documentation":"ChangeLogicalParent includes a trivial loop check: an element cannot be its own parent. If newParent == this, it throws InvalidOperationException(SR.CannotBeSelfParent). This prevents infinite parent chains and illegal tree structure.","triggerScenarios":"Calling ChangeLogicalParent(element) on itself — e.g. accidentally passing 'this' instead of the intended child/parent in custom logical-tree code, or a cycle where a container ends up as its own child.","commonSituations":"Custom panel/content-control code wiring parents; recursion bugs where a node gets added to itself; generic re-parenting utilities mishandling self-references.","solutions":["Verify the arguments before parenting: assert(child != parent)","Skip the call when newParent == this (it is a no-op semantically)","Trace the re-parenting code path for cycles where an ancestor is re-added as a descendant"],"exampleFix":"// before\nchild.ChangeLogicalParent(child);\n// after\nif (child != newParent) child.ChangeLogicalParent(newParent);","handlingStrategy":"validation","validationCode":"if (ReferenceEquals(child, newParent)) throw new ArgumentException(\"An element cannot be its own logical parent.\");","typeGuard":"bool IsNotSelfParent(System.Windows.FrameworkElement child, object newParent) => !ReferenceEquals(child, newParent);","tryCatchPattern":"try { child.ChangeLogicalParent(newParent); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"own parent\")) { /* skip; self-parenting is a no-op */ }","preventionTips":["Assert child != parent before any parenting call","Audit generic reparenting utilities for self-reference cycles","Guard against recursive tree-building code that could re-add ancestors as children"],"tags":["wpf","logical-tree","parenting","cycle"],"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"}