{"record":{"id":"ce6f9466daf4246b","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-format-microsoft-windows","errorCode":null,"errorMessage":"Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidApplicationMenuOrItemContainer, this.GetType().Name, itemContainer)","messagePattern":"Microsoft\\.Windows\\.Controls\\.SR\\.Format\\(Microsoft\\.Windows\\.Controls\\.SR\\.InvalidApplicationMenuOrItemContainer, 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/RibbonApplicationMenu.cs","lineNumber":241,"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 RibbonApplicationMenuItem || itemContainer is RibbonApplicationSplitMenuItem || itemContainer is RibbonSeparator || itemContainer is RibbonGallery)\n                    {\n                        return itemContainer as DependencyObject;\n                    }\n                    else\n                    {\n                        throw new InvalidOperationException(Microsoft.Windows.Controls.SR.Format(Microsoft.Windows.Controls.SR.InvalidApplicationMenuOrItemContainer, this.GetType().Name, itemContainer));\n                    }\n                }\n            }\n\n            return new RibbonApplicationMenuItem();\n        }\n\n        protected override bool ShouldApplyItemContainerStyle(DependencyObject container, object item)\n        {\n            if (container is RibbonApplicationSplitMenuItem ||\n                container is RibbonSeparator ||\n                container is RibbonGallery)\n            {\n                return false;\n            }\n            else\n            {\n                return base.ShouldApplyItemContainerStyle(container, item);","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonApplicationMenu.cs#L223-L259","documentation":"RibbonApplicationMenu.GetContainerForItemOverride validates that an existing item container passed back into the menu is one of the allowed types: RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery. Any other container type throws InvalidOperationException (InvalidApplicationMenuOrItemContainer) naming the menu type and the offending container.","triggerScenarios":"Providing an item container of the wrong type — e.g. returning a custom Button/MenuItem from GetContainerForItemOverride in a derived RibbonApplicationMenu, or binding items whose explicit ItemContainerStyle/type maps to an unsupported container.","commonSituations":"Subclassing RibbonApplicationMenu and overriding container generation, or ItemsSource items with ItemContainerStyle set to non-ribbon containers; template/style retargeting that swaps container types.","solutions":["Ensure container generation produces RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery only.","If overriding GetContainerForItemOverride/IsItemItsOwnContainerOverride in a subclass, restrict accepted containers to the allowed types.","Remove explicit ItemContainerStyle/container assignments that force unsupported container types."],"exampleFix":"// before\nprotected override DependencyObject GetContainerForItemOverride()\n{\n    return new MenuItem(); // unsupported\n}\n// after\nprotected override DependencyObject GetContainerForItemOverride()\n{\n    return new RibbonApplicationMenuItem();\n}","handlingStrategy":"type-guard","validationCode":"bool ok = obj is RibbonApplicationMenuItem || obj is RibbonApplicationSplitMenuItem || obj is RibbonSeparator || obj is RibbonGallery;","typeGuard":"static bool IsValidMenuContainer(object c) => c is RibbonApplicationMenuItem || c is RibbonApplicationSplitMenuItem || c is RibbonSeparator || c is RibbonGallery;","tryCatchPattern":"try { return GetContainerForItemOverride(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"container\")) { return new RibbonApplicationMenuItem(); }","preventionTips":["Override IsItemItsOwnContainerOverride to accept only the four allowed types.","Return RibbonApplicationMenuItem (or SplitMenuItem) from GetContainerForItemOverride overrides.","Keep ItemContainerStyle aligned with Ribbon container types."],"tags":["wpf","ribbon","invalidoperation","itemcontainer"],"backgroundTag":"type-mismatch","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"}