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

RibbonApplicationMenuItem.GetContainerForItemOverride (shared validation used by the application-menu item hierarchy) only accepts existing containers of type RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery; anything else throws InvalidOperationException (InvalidApplicationMenuOrItemContainer) with the type name and container. This keeps the ribbon application menu's item hierarchy homogeneous.

Solutions

  1. Use only RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery as children/containers.
  2. In subclass overrides of IsItemItsOwnContainerOverride, return false for unsupported types so the default container is generated.
  3. Replace plain MenuItems/Buttons inside the ribbon application menu with Ribbon* equivalents.

Example fix

// before
menu.Items.Add(new MenuItem { Header = "Open" });
// after
menu.Items.Add(new RibbonApplicationMenuItem { Header = "Open" });
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(child is RibbonApplicationMenuItem || child is RibbonApplicationSplitMenuItem || child is RibbonSeparator || child is RibbonGallery)) child = new RibbonApplicationMenuItem();

Type guard

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

Try / catch

try { items.Add(child); }
catch (InvalidOperationException ex) when (ex.Message.Contains("container")) { items.Add(new RibbonApplicationMenuItem { DataContext = child }); }

Prevention

When it happens

Trigger: Marking an arbitrary UIElement as the item container (IsItemItsOwnContainerOverride path) or overriding container generation in a derived RibbonApplicationMenuItem/RibbonApplicationSubMenu to return other containers (e.g. plain MenuItem).

Common situations: Custom ribbon menu item subclasses, styles that retemplate items into unsupported container types, or placing unsupported elements directly as children of the application menu.

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/3345db15fde04712. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonApplicationMenuItem.cs:75

        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)