{"record":{"id":"04fd223777266ba9","repo":"dotnet/wpf","slug":"sr-elementmustbeinpopup","errorCode":null,"errorMessage":"SR.ElementMustBeInPopup","messagePattern":"SR\\.ElementMustBeInPopup","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ToolTip.cs","lineNumber":455,"sourceCode":"        /// <summary>\n        /// Creates AutomationPeer (<see cref=\"UIElement.OnCreateAutomationPeer\"/>)\n        /// </summary>\n        protected override AutomationPeer OnCreateAutomationPeer()\n        {\n            return new ToolTipAutomationPeer(this);\n        }\n\n        /// <summary>\n        /// Called when this element's visual parent changes\n        /// </summary>\n        /// <param name=\"oldParent\"></param>\n        protected internal override void OnVisualParentChanged(DependencyObject oldParent)\n        {\n            base.OnVisualParentChanged(oldParent);\n\n            if (!Popup.IsRootedInPopup(_parentPopup, this))\n            {\n                throw new InvalidOperationException(SR.Format(SR.ElementMustBeInPopup, \"ToolTip\"));\n            }\n        }\n\n        internal override void OnAncestorChanged()\n        {\n            base.OnAncestorChanged();\n\n            if (!Popup.IsRootedInPopup(_parentPopup, this))\n            {\n                throw new InvalidOperationException(SR.Format(SR.ElementMustBeInPopup, \"ToolTip\"));\n            }\n        }\n\n        #endregion\n\n        #region Private Methods\n\n        protected override void OnContentChanged(object oldContent, object newContent)","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ToolTip.cs#L437-L473","documentation":"A ToolTip must be rooted inside a Popup. When the ToolTip's visual parent changes (OnVisualParentChanged) and Popup.IsRootedInPopup determines the tooltip is not properly hosted in a Popup, an InvalidOperationException is thrown. ToolTips rely on Popup infrastructure for positioning and display, so attaching one directly into the visual tree elsewhere is invalid.","triggerScenarios":"Assigning a ToolTip instance as a direct child of a panel/control (e.g. somePanel.Children.Add(toolTip)), or otherwise re-parenting a ToolTip outside a Popup, which fires OnVisualParentChanged.","commonSituations":"Confusing ToolTip with a normal control and adding it to a layout; programmatically building tooltips and inserting them into the tree instead of setting the FrameworkElement.ToolTip property.","solutions":["Set the tooltip via element.ToolTip = toolTip instead of adding it as a child.","Host the ToolTip in a Popup via Popup.Child if you need manual control.","Remove code that re-parents the ToolTip; use ToolTipService attached properties for customization."],"exampleFix":"// before\nstackPanel.Children.Add(myToolTip);\n// after\nbutton.ToolTip = myToolTip;","handlingStrategy":"validation","validationCode":"if (element.ToolTip is ToolTip tt)\n{\n    // do not add tt to a visual tree; assign via .ToolTip instead\n}\nelement.ToolTip = myToolTip;","typeGuard":"static bool IsPopupRooted(ToolTip tt) => tt.TemplatedParent is Popup || tt.Parent is Popup || tt.TemplatedParent != null && tt.TemplatedParent is Popup;","tryCatchPattern":"try { panel.Children.Add(control); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"ToolTip\")) { /* assign via control.ToolTip instead */ }","preventionTips":["Never add a ToolTip as a child of a layout panel.","Always assign ToolTips via the FrameworkElement.ToolTip property or ToolTipService.","Create a new ToolTip per element instead of reparenting."],"tags":["wpf","invalid-operation","tooltip","popup"],"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"}