{"record":{"id":"e62e160cd15ed485","repo":"dotnet/wpf","slug":"sr-windowmustberoot","errorCode":null,"errorMessage":"SR.WindowMustBeRoot","messagePattern":"SR\\.WindowMustBeRoot","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1777,"sourceCode":"            // Checking for Visual parent here covers all the scenarios\n            // including the following:\n\n            // Window w1 = new Window();\n            // Window w2 = new WIndow();\n            // w1.Show();\n            // w2.Show();\n            // w1.VisualChildren.Add(w2);\n\n\n            // Window w1 = new Window();\n            // Window w2 = new WIndow();\n            // w1.Show();\n            // w1.VisualChildren.Add(w2);\n            //  w2.Show();\n\n            if ( VisualTreeHelper.GetParent(this) != null )\n            {\n                throw new InvalidOperationException(SR.WindowMustBeRoot);\n            }\n        }\n\n        /// <summary>\n        ///     Measurement override. Implements content sizing logic.\n        /// </summary>\n        /// <remarks>\n        ///     Deducts the frame size from the constraint and then passes it on\n        ///     to its child.  Only supports one Visual child (just like control)\n        /// </remarks>\n        protected override Size MeasureOverride(Size availableSize)\n        {\n            VerifyContextAndObjectState();\n\n            // Window content should respect Window's Max/Min size\n            // setting in a SizeToContent Window.\n            //\n            // Take Min/Max[Width/Height] into consideration.  The logic here is similar to","sourceCodeStart":1759,"sourceCodeEnd":1795,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1759-L1795","documentation":"OnVisualParentChanged throws InvalidOperationException when a Window acquires a visual parent. A Window must always be the root of a visual tree; embedding one as a visual child of another element is unsupported. The check uses VisualTreeHelper.GetParent(this) != null.","triggerScenarios":"Adding a Window instance into another element's visual/logical tree, e.g. panel.Children.Add(otherWindow) or via re-templating that places a Window as a child.","commonSituations":"Developers trying to embed windows as controls in a dashboard layout; code mistaking Window for a UserControl; XAML declaring a Window inside another Window's content.","solutions":["Host embedded UI with UserControl or Page instead of Window.","Show the second window as a separate top-level window (Show/ShowDialog) with an Owner set.","If you need window-like chrome inside content, use a Border/Popup combination."],"exampleFix":"// before\nthis.Content = new OtherWindow(); // Window as content\n// after\nthis.Content = new OtherUserControl();","handlingStrategy":"validation","validationCode":"bool safeToPresent(object content) => !(content is Window); // refuse Window as child content","typeGuard":"bool IsEmbeddableContent(object o) => o is UserControl || o is Page;","tryCatchPattern":null,"preventionTips":["Never add a Window to a panel, content, or tree; use UserControl.","Show extra windows top-level with Owner set.","Review XAML that instantiates Window inside other markup."],"tags":["wpf","window","visual-tree","root"],"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-22T01:17:13.364Z"}