dotnet/wpf · error · InvalidOperationException
Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
Error message
Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidApplicationMenuOrItemContainer, this.GetType().Name, itemContainer)
What it means
RibbonApplicationSplitMenuItem.GetContainerForItemOverride is called by the WPF ItemsControl machinery when it needs a container for an item. If an existing container is supplied (e.g. via ItemContainerStyle or direct Items.Add), it must be a RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery; anything else makes the RibbonApplicationSplitItem menu unable to host it, so an InvalidOperationException is thrown naming the menu type and the offending container.
Solutions
- Use only RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery as direct children of the application menu / split menu item.
- If databinding, set ItemContainerStyle targeting the correct container type (or use ItemTemplate so items are rendered inside a valid container) instead of letting foreign containers be produced.
- If you need different content, wrap it in a valid container's Content/ItemsSource rather than adding it as a raw item.
- Catch InvalidOperationException around container generation only if you are intentionally probing unsupported containers; prefer fixing the item types.
Example fix
// before
<ribbon:RibbonApplicationSplitMenuItem ItemsSource="{Binding Tools}" /> <!-- Tools are plain objects, wrong container -->
// after
<ribbon:RibbonApplicationSplitMenuItem ItemsSource="{Binding Tools}">
<ribbon:RibbonApplicationSplitMenuItem.ItemContainerStyle>
<Style TargetType="ribbon:RibbonApplicationMenuItem" />
</ribbon:RibbonApplicationSplitMenuItem.ItemContainerStyle>
</ribbon:RibbonApplicationSplitMenuItem> Defensive patterns
Strategy: validation
Validate before calling
bool IsValidAppMenuContainer(object item) =>
item is RibbonApplicationMenuItem || item is RibbonApplicationSplitMenuItem ||
item is RibbonSeparator || item is RibbonGallery; Type guard
bool IsValidAppMenuContainer(object item) => item is DependencyObject d && (d is RibbonApplicationMenuItem || d is RibbonApplicationSplitMenuItem || d is RibbonSeparator || d is RibbonGallery);
Prevention
- Restrict direct children of RibbonApplicationSplitMenuItem to the four supported container types.
- Always set ItemContainerStyle with the correct ribbon TargetType when databinding.
- Don't copy plain Menu/ContextMenu XAML into the Ribbon application menu without converting container types.
When it happens
Trigger: Adding an object that is not one of the four allowed container types directly to a RibbonApplicationSplitMenuItem's Items collection, or binding ItemsSource to a collection and setting ItemContainerStyle so a foreign container type (e.g. RibbonButton, MenuItem, TextBlock) is generated/passed as itemContainer.
Common situations: Developers reusing XAML from a regular Menu/MenuItem inside the Ribbon ApplicationMenu, databinding an arbitrary business-object collection without an appropriate ItemContainerStyle, or nesting Ribbon controls of the wrong kind inside RibbonApplicationSplitMenuItem.
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
- Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…
- ' ' can only host a ' ' or a ' '. ' ' is an invalid…
- A panel with IsItemsHost="true" is not nested in an…
- ArgumentOutOfRangeException(parameterName)
- ArgumentOutOfRangeException(parameterName)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e40acfee8e881be4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonApplicationSplitMenuItem.cs:73
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 RibbonApplicationMenuItem || itemContainer is RibbonApplicationSplitMenuItem || itemContainer is RibbonSeparator || itemContainer is RibbonGallery)
{
return itemContainer as DependencyObject;
}
else
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidApplicationMenuOrItemContainer, this.GetType().Name, itemContainer));
}
}
}
return new RibbonApplicationMenuItem();
}
protected override bool ShouldApplyItemContainerStyle(DependencyObject container, object item)
{
if (container is RibbonApplicationSplitMenuItem ||
container is RibbonSeparator ||
container is RibbonGallery)
{
return false;
}
else
{
return base.ShouldApplyItemContainerStyle(container, item);View on GitHub (pinned to 81131a70a4)