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

RibbonContextMenu.GetContainerForItemOverride requires any pre-existing container item to be a RibbonMenuItem, RibbonGallery, or RibbonSeparator; the context menu cannot host other container types. When an incompatible container reaches it, an InvalidOperationException is thrown naming the menu type and the container.

Solutions

  1. Only add RibbonMenuItem, RibbonGallery, or RibbonSeparator as direct items of RibbonContextMenu.
  2. When binding ItemsSource, set ItemContainerStyle with TargetType="ribbon:RibbonMenuItem" so generated containers are valid.
  3. Use ItemTemplate for custom visuals instead of swapping in a different container type.
  4. Refactor code that reuses ContextMenu/MenuItem XAML to use the Ribbon menu types.

Example fix

// before
<ribbon:RibbonContextMenu>
  <ribbon:RibbonButton Label="Cut" /> <!-- invalid container -->
</ribbon:RibbonContextMenu>

// after
<ribbon:RibbonContextMenu>
  <ribbon:RibbonMenuItem Header="Cut" />
</ribbon:RibbonContextMenu>
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidContextMenuContainer(object item) =>
    item is RibbonMenuItem || item is RibbonGallery || item is RibbonSeparator;

Type guard

bool IsValidContextMenuContainer(object item) => item is DependencyObject d && (d is RibbonMenuItem || d is RibbonGallery || d is RibbonSeparator);

Prevention

When it happens

Trigger: Adding a non-RibbonMenuItem/RibbonGallery/RibbonSeparator object (e.g. RibbonButton, MenuItem, a raw string with a foreign container style) directly to a RibbonContextMenu's Items, or databinding with an ItemContainerStyle that yields an unsupported container type.

Common situations: Porting a plain ContextMenu XAML to RibbonContextMenu, generating items from a collection without a proper container style, or programmatically inserting ribbon controls that are not menu containers.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonContextMenu.cs:529

        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 bool ShouldApplyItemContainerStyle(DependencyObject container, object item)
        {
            if (container is RibbonSeparator ||
                container is RibbonGallery)
            {
                return false;
            }
            else
            {
                return base.ShouldApplyItemContainerStyle(container, item);
            }

View on GitHub (pinned to 81131a70a4)