dotnet/wpf · error
A panel with IsItemsHost="true" is not nested in an…
Error message
A panel with IsItemsHost="true" is not nested in an ItemsControl. Panel must be nested in ItemsControl to get and show items.
What it means
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.
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
Example fix
// before
<Grid>
<StackPanel IsItemsHost="true"/> <!-- throws -->
</Grid>
// after
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate><StackPanel IsItemsHost="true"/></ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox> Defensive patterns
Strategy: validation
Validate before calling
var owner = ItemsControl.GetItemsOwner(myPanel);
if (owner == null)
{
// panel is IsItemsHost=true but not under an ItemsControl — fix template/hosting first
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.
Related errors
- Cannot explicitly modify Children collection of Panel used…
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Parameter is unexpected type
- Specified index is out of range or child at index is null…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c027bb2f1857ec89.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Panel.cs:574
return IsItemsHost && _itemContainerGenerator != null;
}
}
/// <summary> Used by subclasses to decide whether to call through a profiling stub </summary>
internal static bool IsAboutToGenerateContent(Panel panel)
{
return panel.IsItemsHost && panel._itemContainerGenerator == null;
}
private void ConnectToGenerator()
{
Debug.Assert(_itemContainerGenerator == null, "Attempted to connect to a generator when Panel._itemContainerGenerator is non-null.");
ItemsControl itemsOwner = ItemsControl.GetItemsOwner(this);
if (itemsOwner == null)
{
// This can happen if IsItemsHost=true, but the panel is not nested in an ItemsControl
throw new InvalidOperationException(SR.Panel_ItemsControlNotFound);
}
IItemContainerGenerator itemsOwnerGenerator = itemsOwner.ItemContainerGenerator;
if (itemsOwnerGenerator != null)
{
_itemContainerGenerator = itemsOwnerGenerator.GetItemContainerGeneratorForPanel(this);
if (_itemContainerGenerator != null)
{
_itemContainerGenerator.ItemsChanged += new ItemsChangedEventHandler(OnItemsChanged);
((IItemContainerGenerator)_itemContainerGenerator).RemoveAll();
}
}
}
private void DisconnectFromGenerator()
{
Debug.Assert(_itemContainerGenerator != null, "Attempted to disconnect from a generator when Panel._itemContainerGenerator is null.");
View on GitHub (pinned to 81131a70a4)