{"record":{"id":"c027bb2f1857ec89","repo":"dotnet/wpf","slug":"a-panel-with-isitemshost-true-is-not-nested-in-an","errorCode":null,"errorMessage":"A panel with IsItemsHost=\"true\" is not nested in an ItemsControl. Panel must be nested in ItemsControl to get and show items.","messagePattern":"A panel with IsItemsHost=\"true\" is not nested in an ItemsControl\\. Panel must be nested in ItemsControl to get and show items\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs","lineNumber":574,"sourceCode":"                return IsItemsHost && _itemContainerGenerator != null;\n            }\n        }\n\n        /// <summary> Used by subclasses to decide whether to call through a profiling stub </summary>\n        internal static bool IsAboutToGenerateContent(Panel panel)\n        {\n            return panel.IsItemsHost && panel._itemContainerGenerator == null;\n        }\n\n        private void ConnectToGenerator()\n        {\n            Debug.Assert(_itemContainerGenerator == null, \"Attempted to connect to a generator when Panel._itemContainerGenerator is non-null.\");\n\n            ItemsControl itemsOwner = ItemsControl.GetItemsOwner(this);\n            if (itemsOwner == null)\n            {\n                // This can happen if IsItemsHost=true, but the panel is not nested in an ItemsControl\n                throw new InvalidOperationException(SR.Panel_ItemsControlNotFound);\n            }\n\n            IItemContainerGenerator itemsOwnerGenerator = itemsOwner.ItemContainerGenerator;\n            if (itemsOwnerGenerator != null)\n            {\n                _itemContainerGenerator = itemsOwnerGenerator.GetItemContainerGeneratorForPanel(this);\n                if (_itemContainerGenerator != null)\n                {\n                    _itemContainerGenerator.ItemsChanged += new ItemsChangedEventHandler(OnItemsChanged);\n                    ((IItemContainerGenerator)_itemContainerGenerator).RemoveAll();\n                }\n            }\n        }\n\n        private void DisconnectFromGenerator()\n        {\n            Debug.Assert(_itemContainerGenerator != null, \"Attempted to disconnect from a generator when Panel._itemContainerGenerator is null.\");\n","sourceCodeStart":556,"sourceCodeEnd":592,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs#L556-L592","documentation":"Panel.ConnectToGenerator is invoked by EnsureGenerator when the panel realizes it must produce item containers. If IsItemsHost=true but ItemsControl.GetItemsOwner(panel) returns null, no owning ItemsControl exists, and the panel throws this InvalidOperationException — an items-host panel is meaningless outside an ItemsControl.","triggerScenarios":"A Panel with IsItemsHost=\"true\" placed outside any ItemsControl (e.g. directly in a Window or Grid, or inside a non-ItemsControl template part), then measured/arranged so the generator initializes.","commonSituations":"Hand-built ControlTemplates where the panel is not named/nested under an ItemsPresenter; copying an items panel template snippet into a plain layout; typos breaking the ItemsPresenter→panel linkage so GetItemsOwner fails.","solutions":["Nest the panel inside an ItemsControl (via ItemsPresenter in the ControlTemplate)","Remove IsItemsHost=\"true\" if the panel is just doing normal layout","Ensure the ControlTemplate contains <ItemsPresenter/> so the panel is associated with the ItemsControl","Use a standard ItemsPanel declaration (ItemsPanelTemplate) rather than manual templating"],"exampleFix":"// before\n<Grid>\n  <StackPanel IsItemsHost=\"true\"/> <!-- throws -->\n</Grid>\n// after\n<ListBox>\n  <ListBox.ItemsPanel>\n    <ItemsPanelTemplate><StackPanel IsItemsHost=\"true\"/></ItemsPanelTemplate>\n  </ListBox.ItemsPanel>\n</ListBox>","handlingStrategy":"validation","validationCode":"var owner = ItemsControl.GetItemsOwner(myPanel);\nif (owner == null)\n{\n    // panel is IsItemsHost=true but not under an ItemsControl — fix template/hosting first\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only set IsItemsHost=true on panels nested in an ItemsControl","Include <ItemsPresenter/> in custom ControlTemplates for items controls","Use ItemsPanelTemplate to specify items panels instead of hand-wiring","Test custom item templates early — this throws at first measure"],"tags":["wpf","panel","itemscontrol"],"backgroundTag":"missing-dependency","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"}