{"record":{"id":"e40acfee8e881be4","repo":"dotnet/wpf","slug":"microsoft-windows-controls-sr-format-microsoft-windows-e40acf","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/RibbonApplicationSplitMenuItem.cs","lineNumber":73,"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":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Windows.Controls.Ribbon/Microsoft/Windows/Controls/Ribbon/RibbonApplicationSplitMenuItem.cs#L55-L91","documentation":"RibbonApplicationSplitMenuItem.GetContainerForItemOverride is called by the WPF ItemsControl machinery when it needs a container for an item. If an existing container is supplied (e.g. via ItemContainerStyle or direct Items.Add), it must be a RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery; anything else makes the RibbonApplicationSplitItem menu unable to host it, so an InvalidOperationException is thrown naming the menu type and the offending container.","triggerScenarios":"Adding an object that is not one of the four allowed container types directly to a RibbonApplicationSplitMenuItem's Items collection, or binding ItemsSource to a collection and setting ItemContainerStyle so a foreign container type (e.g. RibbonButton, MenuItem, TextBlock) is generated/passed as itemContainer.","commonSituations":"Developers reusing XAML from a regular Menu/MenuItem inside the Ribbon ApplicationMenu, databinding an arbitrary business-object collection without an appropriate ItemContainerStyle, or nesting Ribbon controls of the wrong kind inside RibbonApplicationSplitMenuItem.","solutions":["Use only RibbonApplicationMenuItem, RibbonApplicationSplitMenuItem, RibbonSeparator, or RibbonGallery as direct children of the application menu / split menu item.","If databinding, set ItemContainerStyle targeting the correct container type (or use ItemTemplate so items are rendered inside a valid container) instead of letting foreign containers be produced.","If you need different content, wrap it in a valid container's Content/ItemsSource rather than adding it as a raw item.","Catch InvalidOperationException around container generation only if you are intentionally probing unsupported containers; prefer fixing the item types."],"exampleFix":"// before\n<ribbon:RibbonApplicationSplitMenuItem ItemsSource=\"{Binding Tools}\" /> <!-- Tools are plain objects, wrong container -->\n\n// after\n<ribbon:RibbonApplicationSplitMenuItem ItemsSource=\"{Binding Tools}\">\n  <ribbon:RibbonApplicationSplitMenuItem.ItemContainerStyle>\n    <Style TargetType=\"ribbon:RibbonApplicationMenuItem\" />\n  </ribbon:RibbonApplicationSplitMenuItem.ItemContainerStyle>\n</ribbon:RibbonApplicationSplitMenuItem>","handlingStrategy":"validation","validationCode":"bool IsValidAppMenuContainer(object item) =>\n    item is RibbonApplicationMenuItem || item is RibbonApplicationSplitMenuItem ||\n    item is RibbonSeparator || item is RibbonGallery;","typeGuard":"bool IsValidAppMenuContainer(object item) => item is DependencyObject d && (d is RibbonApplicationMenuItem || d is RibbonApplicationSplitMenuItem || d is RibbonSeparator || d is RibbonGallery);","tryCatchPattern":null,"preventionTips":["Restrict direct children of RibbonApplicationSplitMenuItem to the four supported container types.","Always set ItemContainerStyle with the correct ribbon TargetType when databinding.","Don't copy plain Menu/ContextMenu XAML into the Ribbon application menu without converting container types."],"tags":["wpf","ribbon","invalid-container","itemscontrol"],"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"}