dotnet/wpf · error · InvalidOperationException

Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Contr…

Error message

Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidMenuButtonOrItemContainer, this.GetType().Name, itemContainer)

What it means

RibbonMenuButton.GetContainerForItemOverride throws this InvalidOperationException when the container template selected by ItemContainerTemplateSelector produces an element that is not a RibbonMenuItem, RibbonGallery or other expected container type. It fires when a custom ItemContainerTemplate returns the wrong control type for a ribbon menu item, breaking the item-container contract.

Solutions

  1. Ensure the item container template produces a RibbonMenuItem, RibbonGallery, or other supported Ribbon container type
  2. Fix or replace the ItemContainerTemplate registered in ItemContainerTemplateSelector
  3. Do not override the container template; let the control create its default container
  4. If overriding GetContainerForItemOverride, return a valid RibbonMenuButton-compatible container
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonMenuButton.cs:557 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonMenuButton.cs:557

        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 RibbonMenuItem || itemContainer is RibbonGallery || itemContainer is RibbonSeparator)
                    {
                        return itemContainer as DependencyObject;
                    }
                    else
                    {
                        throw new InvalidOperationException(Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidMenuButtonOrItemContainer, this.GetType().Name, itemContainer));
                    }
                }
            }

            return new RibbonMenuItem();
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);

            if (element is RibbonGallery)
            {
                HasGallery = (++_galleryCount > 0);
            }
            else
            {
                RibbonSeparator separator = element as RibbonSeparator;

View on GitHub (pinned to 81131a70a4)