{"record":{"id":"e723671555ff4851","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-format-microsoft-windows-e72367","errorCode":null,"errorMessage":"Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidMenuButtonOrItemContainer, this.GetType().Name, itemContainer)","messagePattern":"Microsoft\\.Windows\\.Controls\\.SR\\.Format\\(Microsoft\\.Windows\\.Controls\\.SR\\.InvalidMenuButtonOrItemContainer, this\\.GetType\\(\\)\\.Name, itemContainer\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonContextMenu.cs","lineNumber":529,"sourceCode":"\n        protected override DependencyObject GetContainerForItemOverride()\n        {\n            object currentItem = _currentItem;\n            _currentItem = null;\n\n            if (UsesItemContainerTemplate)\n            {\n                DataTemplate itemContainerTemplate = ItemContainerTemplateSelector.SelectTemplate(currentItem, this);\n                if (itemContainerTemplate != null)\n                {\n                    object itemContainer = itemContainerTemplate.LoadContent();\n                    if (itemContainer is RibbonMenuItem || itemContainer is RibbonGallery || itemContainer is RibbonSeparator)\n                    {\n                        return itemContainer as DependencyObject;\n                    }\n                    else\n                    {\n                        throw new InvalidOperationException(Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidMenuButtonOrItemContainer, this.GetType().Name, itemContainer));\n                    }\n                }\n            }\n\n            return new RibbonMenuItem();\n        }\n\n        protected override bool ShouldApplyItemContainerStyle(DependencyObject container, object item)\n        {\n            if (container is RibbonSeparator ||\n                container is RibbonGallery)\n            {\n                return false;\n            }\n            else\n            {\n                return base.ShouldApplyItemContainerStyle(container, item);\n            }","sourceCodeStart":511,"sourceCodeEnd":547,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonContextMenu.cs#L511-L547","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only add RibbonMenuItem, RibbonGallery, or RibbonSeparator as direct items of RibbonContextMenu.","When binding ItemsSource, set ItemContainerStyle with TargetType=\"ribbon:RibbonMenuItem\" so generated containers are valid.","Use ItemTemplate for custom visuals instead of swapping in a different container type.","Refactor code that reuses ContextMenu/MenuItem XAML to use the Ribbon menu types."],"exampleFix":"// before\n<ribbon:RibbonContextMenu>\n  <ribbon:RibbonButton Label=\"Cut\" /> <!-- invalid container -->\n</ribbon:RibbonContextMenu>\n\n// after\n<ribbon:RibbonContextMenu>\n  <ribbon:RibbonMenuItem Header=\"Cut\" />\n</ribbon:RibbonContextMenu>","handlingStrategy":"validation","validationCode":"bool IsValidContextMenuContainer(object item) =>\n    item is RibbonMenuItem || item is RibbonGallery || item is RibbonSeparator;","typeGuard":"bool IsValidContextMenuContainer(object item) => item is DependencyObject d && (d is RibbonMenuItem || d is RibbonGallery || d is RibbonSeparator);","tryCatchPattern":null,"preventionTips":["Only add RibbonMenuItem, RibbonGallery, or RibbonSeparator directly to RibbonContextMenu.","Set ItemContainerStyle TargetType=\"ribbon:RibbonMenuItem\" when binding ItemsSource.","Convert existing ContextMenu/MenuItem XAML to ribbon equivalents before hosting it in RibbonContextMenu."],"tags":["wpf","ribbon","invalid-container","contextmenu"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}