dotnet/wpf · error · InvalidOperationException

SR.TemplateNotTargetType

Error message

SR.TemplateNotTargetType

What it means

ItemsPanelTemplate does not allow the XAML parser or code to set a TargetType; its target type is fixed to ItemsPresenter. SetTargetTypeInternal unconditionally throws InvalidOperationException(SR.TemplateNotTargetType), so any attempt to declare a TargetType on an ItemsPanelTemplate fails.

Solutions

  1. Remove the TargetType attribute from the ItemsPanelTemplate element.
  2. Keep only the panel element (e.g. <StackPanel/>) as the content of the ItemsPanelTemplate.
  3. If a custom target type is needed, use a different template kind (ControlTemplate) rather than ItemsPanelTemplate.

Example fix

// before
<ItemsPanelTemplate TargetType="ItemsPresenter">
  <StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
// after
<ItemsPanelTemplate>
  <StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
Defensive patterns

Strategy: validation

Validate before calling

// XAML-time guard: never emit TargetType on ItemsPanelTemplate
foreach (var el in xamlDoc.Descendants().Where(e => e.Name.LocalName == "ItemsPanelTemplate" && e.Attribute("TargetType") != null))
    el.Attribute("TargetType").Remove();

Type guard

bool ItemsPanelTemplateHasNoTargetType(string xamlFragment) => !xamlFragment.Contains("ItemsPanelTemplate") || !System.Text.RegularExpressions.Regex.IsMatch(xamlFragment, "ItemsPanelTemplate[^>]*TargetType");

Try / catch

try { ParseXaml(xaml); } catch (InvalidOperationException ex) when (ex.Message.Contains("TargetType")) { /* strip TargetType attr from ItemsPanelTemplate and retry */ }

Prevention

When it happens

Trigger: Writing <ItemsPanelTemplate TargetType="..."> in XAML, or calling SetTargetTypeInternal / a parser path that assigns TargetType on an ItemsPanelTemplate programmatically.

Common situations: Copy-pasting a ControlTemplate/DataTemplate declaration pattern (which do use TargetType) into an ItemsPanelTemplate; code generators emitting TargetType on all template types.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

        //
        //  Internal Properties
        //
        //-------------------------------------------------------------------

        //
        //  TargetType for ItemsPanelTemplate. This is override is
        //  so FrameworkTemplate can see this property.
        //
        internal override Type TargetTypeInternal
        {
            get {  return DefaultTargetType; }
        }

        // Subclasses must provide a way for the parser to directly set the
        // target type.  For ItemsPanelTemplate, this is not allowed.
        internal override void SetTargetTypeInternal(Type targetType)
        {
            throw new InvalidOperationException(SR.TemplateNotTargetType);
        }

        // Target type of ItemsPanelTemplate is ItemsPresenter
        internal static Type DefaultTargetType
        {
            get { return typeof(ItemsPresenter); }
        }

        #endregion Internal Properties

        #region Internal Methods

        //-------------------------------------------------------------------
        //
        //  Internal Methods
        //
        //-------------------------------------------------------------------

View on GitHub (pinned to 81131a70a4)