dotnet/wpf · error
' ' can only host a ' ' or a ' '. ' ' is an invalid…
Error message
'{0}' can only host a '{1}' or a '{2}'. '{3}' is an invalid container. What it means
MenuBase (Menu/ContextMenu) item-container generation requires each container to be a MenuItem or Separator. When container generation produces (or is given) a container of any other type in GetContainerForItemOverride, it throws InvalidOperationException naming the invalid container type.
Solutions
- Ensure all items in the menu are MenuItem or Separator, or use plain data items styled via ItemContainerStyle
- If items are custom objects, rely on default container generation instead of pre-building wrong containers
- If subclassing, override GetContainerForItemOverride to return a MenuItem and IsItemItsOwnContainerOverride to accept only MenuItem/Separator
Example fix
// before
menu.Items.Add(new Button { Content = "Click" });
// after
var mi = new MenuItem { Header = "Click" };
menu.Items.Add(mi); Defensive patterns
Strategy: validation
Validate before calling
foreach (var item in menu.Items.OfType<UIElement>())
if (!(item is MenuItem) && !(item is Separator)) { /* replace or wrap item */ } Type guard
static bool IsValidMenuContainer(object container) => container is MenuItem || container is Separator;
Try / catch
try { /* force container generation, e.g. via Items.Refresh */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("invalid container")) { /* fix items/overrides */ } Prevention
- Only add MenuItem/Separator (or plain data items) to Menu/ContextMenu Items
- If overriding container generation, keep IsItemItsOwnContainerOverride/GetContainerForItemOverride consistent
- Use ItemContainerStyle rather than pre-built wrong containers
When it happens
Trigger: Supplying items that are already UIElements of the wrong kind (so they are used as their own containers), overriding GetContainerForItemOverride to return something other than MenuItem/Separator, or adding non-MenuItem controls directly to a Menu's Items.
Common situations: Putting a Button/TextBlock directly into a Menu's Items, custom ItemContainerStyle/ContainerStyle mismatch, or subclassing Menu with an incompatible container override.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- Microsoft.Windows.Controls.SR.UIA_OperationCannotBePerformed
- (no message) InvalidOperationException()
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/afaec8dcb9d065d4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/MenuBase.cs:504
protected override DependencyObject GetContainerForItemOverride()
{
object currentItem = _currentItem;
_currentItem = null;
if (UsesItemContainerTemplate)
{
DataTemplate itemContainerTemplate = ItemContainerTemplateSelector.SelectTemplate(currentItem, this);
if (itemContainerTemplate != null)
{
object itemContainer = itemContainerTemplate.LoadContent();
if (itemContainer is MenuItem || itemContainer is Separator)
{
return itemContainer as DependencyObject;
}
else
{
throw new InvalidOperationException(SR.Format(SR.InvalidItemContainer, this.GetType().Name, nameof(MenuItem), nameof(Separator), itemContainer));
}
}
}
return new MenuItem();
}
#endregion
//-------------------------------------------------------------------
//
// Private Methods
//
//-------------------------------------------------------------------
#region Private Methods
/// <summary>View on GitHub (pinned to 81131a70a4)