{"record":{"id":"9af996a9f16885a7","repo":"dotnet/wpf","slug":"sr-format-sr-itemspanelnotapanel-templateholder-roottype","errorCode":null,"errorMessage":"SR.Format(SR.ItemsPanelNotAPanel, templateHolder.RootType)","messagePattern":"SR\\.Format\\(SR\\.ItemsPanelNotAPanel, templateHolder\\.RootType\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsPanelTemplate.cs","lineNumber":117,"sourceCode":"        //      a. root element is a Panel\n        // 2. Set IsItemsHost = true\n        //\n\n        internal override void ProcessTemplateBeforeSeal()\n        {\n            FrameworkElementFactory root;\n\n            if( HasContent )\n            {\n                // This is a Baml-style template\n\n                // Validate the root type (it must be a Panel)\n\n                TemplateContent templateHolder = Template as TemplateContent;\n                System.Xaml.XamlType panelType = templateHolder.SchemaContext.GetXamlType(typeof(Panel));\n                if (templateHolder.RootType == null || !templateHolder.RootType.CanAssignTo(panelType))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.ItemsPanelNotAPanel, templateHolder.RootType));\n                }\n            }\n\n            else if ((root = this.VisualTree) != null)\n            {\n                // This is a FEF-style template\n                if (!typeof(Panel).IsAssignableFrom(root.Type))\n                    throw new InvalidOperationException(SR.Format(SR.ItemsPanelNotAPanel, root.Type));\n\n                root.SetValue(Panel.IsItemsHostProperty, true);\n            }\n        }\n\n\n        #endregion Internal Methods\n\n        #region Protected Methods\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsPanelTemplate.cs#L99-L135","documentation":"The root element of an ItemsPanelTemplate's visual tree must derive from System.Windows.Controls.Panel. During ProcessTemplateBeforeSeal, if the template content's root type is null or cannot be assigned to Panel, WPF throws InvalidOperationException naming the offending root type.","triggerScenarios":"Placing a non-Panel element (e.g. <Grid> is fine, but <Border>, <TextBlock>, <ContentPresenter>, or a Control) as the direct child of ItemsPanelTemplate; an empty or malformed template body leaving RootType null.","commonSituations":"Copy-pasting a DataTemplate's content (often a Border/TextBlock) into ItemsPanelTemplate; typos or wrong xmlns producing a null root type; wrapping the panel in a decorator.","solutions":["Make the root element of the ItemsPanelTemplate a Panel (StackPanel, VirtualizingStackPanel, WrapPanel, UniformGrid, Grid, etc.).","If decoration is needed, move it out of the ItemsPanelTemplate into the ItemTemplate or container style.","Fix malformed XAML so the template body parses to a concrete root type."],"exampleFix":"// before\n<ItemsPanelTemplate>\n  <Border><StackPanel/></Border>\n</ItemsPanelTemplate>\n// after\n<ItemsPanelTemplate>\n  <StackPanel/>\n</ItemsPanelTemplate>","handlingStrategy":"validation","validationCode":"bool IsPanelRoot(ItemsPanelTemplate t) =>\n    (t?.VisualTree?.GetType() ?? t?.Template?.RootType?.UnderlyingType)?.IsSubclassOf(typeof(System.Windows.Controls.Panel)) == true;","typeGuard":"bool IsPanel(Type t) => t != null && typeof(System.Windows.Controls.Panel).IsAssignableFrom(t);","tryCatchPattern":"try { itemsControl.ItemsPanel = (ItemsPanelTemplate)Resources[\"ItemsPanel\"]; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"panel\")) { itemsControl.ItemsPanel = new ItemsPanelTemplate { VisualTree = new FrameworkElementFactory(typeof(StackPanel)) }; }","preventionTips":["Always make the ItemsPanelTemplate root a Panel-derived element.","Do not wrap the panel in Border/Decorator inside ItemsPanelTemplate.","Use VirtualizingStackPanel as the root for large virtualized lists."],"tags":["wpf","itemspaneltemplate","panel","xaml"],"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-21T21:30:21.729Z"}