{"record":{"id":"2f068d580b9f55a5","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-format-microsoft-windows-2f068d","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/RibbonMenuButton.cs","lineNumber":557,"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 void PrepareContainerForItemOverride(DependencyObject element, object item)\n        {\n            base.PrepareContainerForItemOverride(element, item);\n\n            if (element is RibbonGallery)\n            {\n                HasGallery = (++_galleryCount > 0);\n            }\n            else\n            {\n                RibbonSeparator separator = element as RibbonSeparator;","sourceCodeStart":539,"sourceCodeEnd":575,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonMenuButton.cs#L539-L575","documentation":"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.","triggerScenarios":"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.","commonSituations":"See trigger scenarios.","solutions":["Ensure the item container template produces a RibbonMenuItem, RibbonGallery, or other supported Ribbon container type","Fix or replace the ItemContainerTemplate registered in ItemContainerTemplateSelector","Do not override the container template; let the control create its default container","If overriding GetContainerForItemOverride, return a valid RibbonMenuButton-compatible container"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}