{"record":{"id":"afaec8dcb9d065d4","repo":"dotnet/wpf","slug":"0-can-only-host-a-1-or-a-2-3-is-an-invalid-container-afaec8","errorCode":null,"errorMessage":"'{0}' can only host a '{1}' or a '{2}'. '{3}' is an invalid container.","messagePattern":"'(.+?)' can only host a '(.+?)' or a '(.+?)'\\. '(.+?)' is an invalid container\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/MenuBase.cs","lineNumber":504,"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 MenuItem || itemContainer is Separator)\n                    {\n                        return itemContainer as DependencyObject;\n                    }\n                    else\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.InvalidItemContainer, this.GetType().Name, nameof(MenuItem), nameof(Separator), itemContainer));\n                    }\n                }\n            }\n\n            return new MenuItem();\n        }\n\n        #endregion\n\n        //-------------------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //-------------------------------------------------------------------\n\n        #region Private Methods\n\n        /// <summary>","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/MenuBase.cs#L486-L522","documentation":"MenuBase (Menu/ContextMenu) item-container generation requires each container to be a MenuItem or Separator. When container generation produces (or is given) a container of any other type in GetContainerForItemOverride, it throws InvalidOperationException naming the invalid container type.","triggerScenarios":"Supplying items that are already UIElements of the wrong kind (so they are used as their own containers), overriding GetContainerForItemOverride to return something other than MenuItem/Separator, or adding non-MenuItem controls directly to a Menu's Items.","commonSituations":"Putting a Button/TextBlock directly into a Menu's Items, custom ItemContainerStyle/ContainerStyle mismatch, or subclassing Menu with an incompatible container override.","solutions":["Ensure all items in the menu are MenuItem or Separator, or use plain data items styled via ItemContainerStyle","If items are custom objects, rely on default container generation instead of pre-building wrong containers","If subclassing, override GetContainerForItemOverride to return a MenuItem and IsItemItsOwnContainerOverride to accept only MenuItem/Separator"],"exampleFix":"// before\nmenu.Items.Add(new Button { Content = \"Click\" });\n// after\nvar mi = new MenuItem { Header = \"Click\" };\nmenu.Items.Add(mi);","handlingStrategy":"validation","validationCode":"foreach (var item in menu.Items.OfType<UIElement>())\n    if (!(item is MenuItem) && !(item is Separator)) { /* replace or wrap item */ }","typeGuard":"static bool IsValidMenuContainer(object container) => container is MenuItem || container is Separator;","tryCatchPattern":"try { /* force container generation, e.g. via Items.Refresh */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"invalid container\")) { /* fix items/overrides */ }","preventionTips":["Only add MenuItem/Separator (or plain data items) to Menu/ContextMenu Items","If overriding container generation, keep IsItemItsOwnContainerOverride/GetContainerForItemOverride consistent","Use ItemContainerStyle rather than pre-built wrong containers"],"tags":["wpf","menu","itemcontainer"],"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"}