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
- Remove the TargetType attribute from the ItemsPanelTemplate element.
- Keep only the panel element (e.g. <StackPanel/>) as the content of the ItemsPanelTemplate.
- 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
- Never write TargetType on ItemsPanelTemplate; only ControlTemplate/DataTemplate declarations need it.
- Lint XAML for TargetType attributes on ItemsPanelTemplate elements.
- Remember ItemsPanelTemplate's target is fixed to ItemsPresenter.
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
- SR.Format(SR.ItemsPanelNotAPanel, root.Type)
- SR.Format(SR.ItemsPanelNotAPanel, templateHolder.RootType)
- Animation_ChildMustBeKeyFrame
- Animation_ChildMustBeKeyFrame
- Animation_NoTextChildren
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)