{"record":{"id":"aed364b085949af9","repo":"dotnet/wpf","slug":"sr-createrootpopup-childhasvisualparent","errorCode":null,"errorMessage":"SR.CreateRootPopup_ChildHasVisualParent","messagePattern":"SR\\.CreateRootPopup_ChildHasVisualParent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Popup.cs","lineNumber":895,"sourceCode":"        /// <param name=\"child\">The element to be the child of the popup.</param>\n        /// <param name=\"bindTreatMousePlacementAsBottomProperty\">Whether to bind TreatMousePlacementAsBottomProperty to the child's FromKeyboard property</param>\n        internal static void CreateRootPopupInternal(Popup popup, UIElement child, bool bindTreatMousePlacementAsBottomProperty)\n        {\n            ArgumentNullException.ThrowIfNull(popup);\n            ArgumentNullException.ThrowIfNull(child);\n\n            Debug.Assert(!bindTreatMousePlacementAsBottomProperty || child is ToolTip, \"child must be a Tooltip to bind TreatMousePlacementAsBottomProperty\");\n\n            // When we get here, the Child must not have already been visually or logically parented.\n            object currentParent = null;\n            if ((currentParent = LogicalTreeHelper.GetParent(child)) != null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CreateRootPopup_ChildHasLogicalParent, child, currentParent));\n            }\n\n            if ((currentParent = VisualTreeHelper.GetParent(child)) != null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CreateRootPopup_ChildHasVisualParent, child, currentParent));\n            }\n\n            // PlacementTarget must be set before hooking up the child so that resource\n            // lookups can work.  The Popup for tooltip and context menu isn't in the tree\n            // so FE relies on GetUIParentCore to return the placement target as the\n            // effective logical parent\n            Binding binding = new Binding(\"PlacementTarget\")\n            {\n                Mode = BindingMode.OneWay,\n                Source = child\n            };\n            popup.SetBinding(PlacementTargetProperty, binding);\n\n            // NOTE: this will hook up child as a logical child of Popup.\n            // If at a later date this is not desired, then modify the hookup to avoid the logical hookup.\n            //\n            // NOTE: Logical linking is necessary if property invalidations are to propagate down\n            // the tree into the child (unless at a later date an alternate method has been created).","sourceCodeStart":877,"sourceCodeEnd":913,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Popup.cs#L877-L913","documentation":"Sibling check in Popup.CreateRootPopupInternal: after verifying there is no logical parent, it also verifies the child has no visual parent (VisualTreeHelper.GetParent). An element already in a visual tree cannot be adopted as a popup root, so WPF throws InvalidOperationException. This typically fires when the element was rendered somewhere else.","triggerScenarios":"Assigning an element that is live in another visual tree (e.g. rendered inside a Window, ControlTemplate, or another Popup) as the Child of a Popup, ToolTip, or ContextMenu via CreateRootPopup/Child setter.","commonSituations":"Moving a visible control into a popup at runtime; sharing one Border/Grid element between a window and a popup; reusing template parts.","solutions":["Remove the element from its current visual parent (e.g. panel.Children.Remove(el)) before assigning it as the popup Child.","Clone or instantiate a new element instead of moving the rendered one.","Check VisualTreeHelper.GetParent(child) == null in a debug assertion before assigning Child."],"exampleFix":"// before\npopup.Child = this.someGrid; // someGrid still in window's visual tree\n// after\nrootPanel.Children.Remove(this.someGrid);\npopup.Child = this.someGrid;","handlingStrategy":"validation","validationCode":"if (VisualTreeHelper.GetParent(child) != null)\n    throw new InvalidOperationException(\"Element still has a visual parent\");","typeGuard":"bool HasVisualParent(UIElement el) => VisualTreeHelper.GetParent(el) != null;","tryCatchPattern":"try { popup.Child = element; }\ncatch (InvalidOperationException) {\n    (VisualTreeHelper.GetParent(element) as Panel)?.Children.Remove(element);\n    popup.Child = element;\n}","preventionTips":["Remove the element from its containing panel before moving it into a popup","Avoid keeping references to rendered elements for reuse elsewhere","Assert child is parentless in debug builds"],"tags":["wpf","popup","visual-tree","parent-conflict"],"backgroundTag":"invalid-argument-value","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"}