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

RibbonApplicationMenu.GetContainerForItemOverride validates that an existing item container passed back into the menu is one of the allowed types: RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery. Any other container type throws InvalidOperationException (InvalidApplicationMenuOrItemContainer) naming the menu type and the offending container.

Solutions

  1. Ensure container generation produces RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery only.
  2. If overriding GetContainerForItemOverride/IsItemItsOwnContainerOverride in a subclass, restrict accepted containers to the allowed types.
  3. Remove explicit ItemContainerStyle/container assignments that force unsupported container types.

Example fix

// before
protected override DependencyObject GetContainerForItemOverride()
{
    return new MenuItem(); // unsupported
}
// after
protected override DependencyObject GetContainerForItemOverride()
{
    return new RibbonApplicationMenuItem();
}
Defensive patterns

Strategy: type-guard

Validate before calling

bool ok = obj is RibbonApplicationMenuItem || obj is RibbonApplicationSplitMenuItem || obj is RibbonSeparator || obj is RibbonGallery;

Type guard

static bool IsValidMenuContainer(object c) => c is RibbonApplicationMenuItem || c is RibbonApplicationSplitMenuItem || c is RibbonSeparator || c is RibbonGallery;

Try / catch

try { return GetContainerForItemOverride(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("container")) { return new RibbonApplicationMenuItem(); }

Prevention

When it happens

Trigger: Providing an item container of the wrong type — e.g. returning a custom Button/MenuItem from GetContainerForItemOverride in a derived RibbonApplicationMenu, or binding items whose explicit ItemContainerStyle/type maps to an unsupported container.

Common situations: Subclassing RibbonApplicationMenu and overriding container generation, or ItemsSource items with ItemContainerStyle set to non-ribbon containers; template/style retargeting that swaps container types.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ce6f9466daf4246b. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonApplicationMenu.cs:241

        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)