{"record":{"id":"a9c896ee4b3e4176","repo":"dotnet/wpf","slug":"sr-cannotbeinsidepopup","errorCode":null,"errorMessage":"SR.CannotBeInsidePopup","messagePattern":"SR\\.CannotBeInsidePopup","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs","lineNumber":739,"sourceCode":"        // Private Methods\n        //\n        //----------------------------------------------\n\n        #region Private Methods\n        \n        private void LoadedHandler(object sender, RoutedEventArgs args)\n        {\n            PresentationSource pSource = PresentationSource.CriticalFromVisual(this);\n\n            // Note that we cannot assert this condition here. The reason is that this element might have \n            // been disconnected from the tree through one of its parents even while it waited for the \n            // pending Loaded event to fire. More details for this scenario can be found in the \n            // Windows OS Bug#1981485.\n            // Invariant.Assert(pSource != null, \"Loaded has fired. PresentationSource shouldn't be null\");\n            \n            if (pSource != null && pSource.RootVisual is PopupRoot)\n            {\n                throw new InvalidOperationException(SR.CannotBeInsidePopup);\n            }\n        }\n\n\n        // Turn on all the WebOC Feature Control Keys implementing various security mitigations. \n        // Whenever possible, we do it programmatically instead of adding reg-keys so that these are on on all WPF apps. \n        // Unfortunately, some FCKs, especially newer ones, work only through the registry.\n        [SuppressMessage(\"Microsoft.Usage\", \"CA1806:DoNotIgnoreMethodResults\", MessageId=\"MS.Win32.UnsafeNativeMethods.CoInternetSetFeatureEnabled(System.Int32,System.Int32,System.Boolean)\", \n            Justification=\"CoInternetSetFeatureEnabled() returns error for an unknown FCK. We expect this to happen with older versions of IE.\")]\n        private static void TurnOnFeatureControlKeys()\n        {\n            Version osver = Environment.OSVersion.Version;\n            if (osver.Major == 5 && osver.Minor == 2 && osver.MajorRevision == 0) \n            {\n                // XPSP2 mitigations - not available on Server 2003 before SP1. \n                return ; \n            }\n","sourceCodeStart":721,"sourceCodeEnd":757,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs#L721-L757","documentation":"WebBrowser throws this InvalidOperationException when it is loaded inside a Popup's visual tree. The WebBrowser control hosts the full HwndSource-based WebBrowser ActiveX control, which cannot function correctly when hosted within a PopupRoot, so WPF explicitly forbids the combination. The check runs in the Loaded handler once the PresentationSource is available.","triggerScenarios":"Placing a <WebBrowser> element inside a <Popup> (or inside any control whose visual root becomes a PopupRoot) and letting it load; the exception is thrown from the Loaded event handler when pSource.RootVisual is PopupRoot.","commonSituations":" Developers building tooltip-like or flyout UI containing a WebBrowser; embedding web content in popup-styled custom controls; migrating code that worked with a hidden window into a Popup in .NET 4+.","solutions":["Move the WebBrowser out of the Popup — host it in a separate always-open Window (e.g. borderless, non-activating) positioned over the anchor instead of using a Popup.","Use an overlay Adorner/Panel within the main window visual tree instead of a Popup.","If the content is simple HTML, render it with a Frame/FlowDocument or third-party control that supports Popup hosting.","P/Invoke-based alternatives (e.g. hosting via WindowsFormsHost outside the popup, or WebView2 with proper hwnd placement) that manage their own top-level hwnd."],"exampleFix":"// before\n<Popup>\n  <WebBrowser Source=\"https://example.com\" />\n</Popup>\n// after\n<Window x:Class=\"FlyoutWindow\" WindowStyle=\"None\" AllowsTransparency=\"False\" ShowInTaskbar=\"False\">\n  <WebBrowser Source=\"https://example.com\" />\n</Window>\n<!-- position FlyoutWindow near the anchor instead of using a Popup -->","handlingStrategy":"try-catch","validationCode":"// before loading a WebBrowser, verify it is not inside a Popup visual tree\nstatic bool IsInsidePopup(System.Windows.DependencyObject d)\n{\n    while (d != null)\n    {\n        d = System.Windows.Media.VisualTreeHelper.GetParent(d);\n        if (d is System.Windows.Controls.Primitives.Popup) return true;\n    }\n    return false;\n}","typeGuard":"static bool InPopupRoot(Visual v) =>\n    (PresentationSource.FromVisual(v)?.RootVisual) is PopupRoot;","tryCatchPattern":"try { popup.Child = new WebBrowser(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"popup\")) { Log.Warn(\"WebBrowser cannot be hosted in a Popup; use a borderless window instead.\"); }","preventionTips":["Never place WebBrowser (or Frame navigating to HTML) inside a Popup.","For flyout web content, use a borderless, non-activating Window positioned over the anchor.","Test popup-heavy UI on .NET 4+ where this restriction is enforced.","Prefer Adorners within the main window for overlay UI."],"tags":["wpf","webbrowser","popup","invalid-operation"],"backgroundTag":"unsupported-operation","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"}