dotnet/wpf · error · NotSupportedException

SR.ToolBar_InvalidStyle_ToolBarOverflowPanel

Error message

SR.ToolBar_InvalidStyle_ToolBarOverflowPanel

What it means

ToolBar.FindToolBarOverflowPanel looks up the template child named ToolBarOverflowPanelTemplateName and requires it to be a ToolBarOverflowPanel. If a template element with that name has a different type, it throws NotSupportedException (SR.ToolBar_InvalidStyle_ToolBarOverflowPanel) naming the actual type. ToolBar needs this specific panel type for its overflow drop-down behavior.

Solutions

  1. Ensure the template child named PART_ToolBarOverflowPanel is of type ToolBarOverflowPanel.
  2. Remove or rename the incorrectly typed element so it does not claim the required template-part name.
  3. Restore the part structure from the default ToolBar template shipped with the active theme.

Example fix

<!-- before -->
<ControlTemplate TargetType="ToolBar">
  ...
  <StackPanel x:Name="PART_ToolBarOverflowPanel"/>
</ControlTemplate>

<!-- after -->
<ControlTemplate TargetType="ToolBar">
  ...
  <ToolBarOverflowPanel x:Name="PART_ToolBarOverflowPanel"/>
</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

// Validate after template application
var overflow = template.FindName("PART_ToolBarOverflowPanel", toolBar) as ToolBarOverflowPanel;
if (overflow == null) throw new InvalidOperationException("Template must declare PART_ToolBarOverflowPanel as ToolBarOverflowPanel");

Type guard

bool ok = VisualTreeHelper.GetChild(toolBar, i) is ToolBarOverflowPanel;

Try / catch

try
{
    toolBar.ApplyTemplate();
}
catch (NotSupportedException ex)
{
    // log template part type error; revert to default ToolBar style
}

Prevention

When it happens

Trigger: Applying a custom ControlTemplate for ToolBar that declares a child named "PART_ToolBarOverflowPanel" whose type is not ToolBarOverflowPanel (e.g. a StackPanel, DockPanel, or ContentPresenter).

Common situations: Custom themes/restyled ToolBars where the overflow panel was swapped for a simpler panel; template refactoring tools or copy-paste edits renaming elements; learning templates from older themes that are then modified.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/df4cdbaa56b3ec86. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ToolBar.cs:619

        /// Gets reference to ToolBar's ToolBarOverflowPanel element.
        /// </summary>
        internal ToolBarOverflowPanel ToolBarOverflowPanel
        {
            get
            {
                if (_toolBarOverflowPanel == null)
                    _toolBarOverflowPanel = FindToolBarOverflowPanel();

                return _toolBarOverflowPanel;
            }
        }

        private ToolBarOverflowPanel FindToolBarOverflowPanel()
        {
            DependencyObject child = GetTemplateChild(ToolBarOverflowPanelTemplateName);
            ToolBarOverflowPanel toolBarOverflowPanel = child as ToolBarOverflowPanel;
            if (child != null && toolBarOverflowPanel == null)
                throw new NotSupportedException(SR.Format(SR.ToolBar_InvalidStyle_ToolBarOverflowPanel, child.GetType()));
            return toolBarOverflowPanel;
        }

        /// <summary>
        /// This is the method that responds to the KeyDown event.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            UIElement newFocusElement = null;
            UIElement currentFocusElement = e.Source as UIElement;
            if (currentFocusElement != null && ItemsControl.ItemsControlFromItemContainer(currentFocusElement) == this)
            {
                // itemsHost should be either ToolBarPanel or ToolBarOverflowPanel
                Panel itemsHost = VisualTreeHelper.GetParent(currentFocusElement) as Panel;
                if (itemsHost != null)
                {
                    switch (e.Key)

View on GitHub (pinned to 81131a70a4)