dotnet/wpf · error · ArgumentException

SR.Format(SR.TemplateTargetTypeMismatch, "ItemsPresenter"…

Error message

SR.Format(SR.TemplateTargetTypeMismatch, "ItemsPresenter", templatedParent.GetType().Name)

What it means

ItemsPanelTemplate can only be applied to an ItemsPresenter. ValidateTemplatedParent throws ArgumentException when FindTemplatedParent/ApplyTemplate supplies a templatedParent that is not an ItemsPresenter, because ItemsPanelTemplate is semantically tied to the presenter that hosts generated items.

Solutions

  1. Apply ItemsPanelTemplate only via ItemsControl.ItemsPanel (or to a real ItemsPresenter).
  2. If you meant to restyle the whole control, use a ControlTemplate instead of ItemsPanelTemplate.
  3. In custom controls, ensure the templated parent at ApplyTemplate time is an ItemsPresenter (place <ItemsPresenter/> in the control template).

Example fix

<!-- before -->
<Style TargetType="ListBox">
  <Setter Property="Template">
    <Setter.Value>
      <ItemsPanelTemplate>...</ItemsPanelTemplate>
    </Setter.Value>
  </Setter>
</Style>
<!-- after -->
<Style TargetType="ListBox">
  <Setter Property="ItemsPanel">
    <Setter.Value>
      <ItemsPanelTemplate>...</ItemsPanelTemplate>
    </Setter.Value>
  </Setter>
</Style>
Defensive patterns

Strategy: type-guard

Validate before calling

if (templatedParent is not ItemsPresenter)
    throw new ArgumentException($"ItemsPanelTemplate must target an ItemsPresenter, got {templatedParent?.GetType().Name}");

Type guard

static bool CanApplyItemsPanelTemplate(DependencyObject parent) => parent is ItemsPresenter;

Try / catch

try { element.Template = itemsPanelTemplate; }
catch (ArgumentException ex) when (ex.Message.Contains("ItemsPresenter")) { /* switch to ControlTemplate for non-presenter targets */ }

Prevention

When it happens

Trigger: Applying an ItemsPanelTemplate to a control other than ItemsPresenter, e.g. assigning it to TemplateProperty of a ListBox/ComboBox/ContentPresenter, calling FrameworkElement.ApplyTemplate with a mismatched template, or setting ItemsPanelTemplate as Template in styles/finders.

Common situations: Confusing Template= with ItemsPanel= in a style; using ItemsPanelTemplate where ControlTemplate is expected in a custom control's default style; programmatic template application on the wrong element.

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/a1873a98aad9ad65. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsPanelTemplate.cs:155

        //
        //  Protected Methods
        //
        //-------------------------------------------------------------------

        /// <summary>
        ///     Validate against the following rules
        ///     1. Must have a non-null feTemplatedParent
        ///     2. A ItemsPanelTemplate must be applied to a ContentPresenter
        /// </summary>
        protected override void ValidateTemplatedParent(FrameworkElement templatedParent)
        {
            // Must have a non-null feTemplatedParent
            ArgumentNullException.ThrowIfNull(templatedParent);

            // A ItemsPanelTemplate must be applied to an ItemsPresenter
            if (!(templatedParent is ItemsPresenter))
            {
                throw new ArgumentException(SR.Format(SR.TemplateTargetTypeMismatch, "ItemsPresenter", templatedParent.GetType().Name));
            }
        }

        #endregion Protected Methods
    }
}


View on GitHub (pinned to 81131a70a4)