{"record":{"id":"ed094b2881b663f4","repo":"dotnet/wpf","slug":"sr-toolbar-invalidstyle-toolbarpanel","errorCode":null,"errorMessage":"SR.ToolBar_InvalidStyle_ToolBarPanel","messagePattern":"SR\\.ToolBar_InvalidStyle_ToolBarPanel","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ToolBar.cs","lineNumber":596,"sourceCode":"        /// Gets reference to ToolBar's ToolBarPanel element.\n        /// </summary>\n        internal ToolBarPanel ToolBarPanel\n        {\n            get\n            {\n                if (_toolBarPanel == null)\n                    _toolBarPanel = FindToolBarPanel();\n\n                return _toolBarPanel;\n            }\n        }\n\n        private ToolBarPanel FindToolBarPanel()\n        {\n            DependencyObject child = GetTemplateChild(ToolBarPanelTemplateName);\n            ToolBarPanel toolBarPanel = child as ToolBarPanel;\n            if (child != null && toolBarPanel == null)\n                throw new NotSupportedException(SR.Format(SR.ToolBar_InvalidStyle_ToolBarPanel, child.GetType()));\n            return toolBarPanel;\n        }\n\n        /// <summary>\n        /// Gets reference to ToolBar's ToolBarOverflowPanel element.\n        /// </summary>\n        internal ToolBarOverflowPanel ToolBarOverflowPanel\n        {\n            get\n            {\n                if (_toolBarOverflowPanel == null)\n                    _toolBarOverflowPanel = FindToolBarOverflowPanel();\n\n                return _toolBarOverflowPanel;\n            }\n        }\n\n        private ToolBarOverflowPanel FindToolBarOverflowPanel()","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ToolBar.cs#L578-L614","documentation":"ToolBar.FindToolBarPanel looks up the template child named ToolBarPanelTemplateName and requires it to be a ToolBarPanel. If the template contains an element with that name that is not a ToolBarPanel, it throws NotSupportedException (SR.ToolBar_InvalidStyle_ToolBarPanel) naming the actual type. The library throws because ToolBar layout depends on ToolBarPanel's special overflow logic.","triggerScenarios":"Applying a custom ControlTemplate for ToolBar (via Style/Template or theme change) that includes a child named \"PART_ToolBarPanel\" (ToolBarPanelTemplateName) whose type is not ToolBarPanel.","commonSituations":"Hand-written or copied custom ToolBar templates where PART_ToolBarPanel is a StackPanel/Grid/Canvas; renaming or retyping the part during restyling; migrating templates across WPF versions where template parts contract is not respected.","solutions":["In the custom ToolBar template, make the element named PART_ToolBarPanel an actual ToolBarPanel.","Remove the PART_ToolBarPanel-named child entirely if you do not want the default behavior, rather than giving it the wrong type.","Compare against the default Aero/Fluent ToolBar template and restore the correct part types."],"exampleFix":"<!-- before -->\n<ControlTemplate TargetType=\"ToolBar\">\n  <StackPanel x:Name=\"PART_ToolBarPanel\" IsItemsHost=\"True\"/>\n</ControlTemplate>\n\n<!-- after -->\n<ControlTemplate TargetType=\"ToolBar\">\n  <ToolBarPanel x:Name=\"PART_ToolBarPanel\" IsItemsHost=\"True\"/>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"// In template authoring/CI: assert required part types\nvar panel = template.FindName(\"PART_ToolBarPanel\", toolBar) as ToolBarPanel;\nif (panel == null) throw new InvalidOperationException(\"Template must declare PART_ToolBarPanel as ToolBarPanel\");","typeGuard":"bool ok = VisualTreeHelper.GetChild(toolBar, i) is ToolBarPanel;","tryCatchPattern":"try\n{\n    toolBar.ApplyTemplate();\n}\ncatch (NotSupportedException ex)\n{\n    // log template part type error; revert to default ToolBar style\n}","preventionTips":["Respect WPF template-part contracts: PART_ToolBarPanel must be ToolBarPanel.","Start custom templates from the default theme template and only restyle, don't retype, parts.","Smoke-test custom control templates on app startup (force ApplyTemplate) in CI."],"tags":["wpf","xaml","control-template","toolbar","template-parts"],"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"}