dotnet/wpf · error · InvalidOperationException

SR.TemplateNotTargetType

Error message

SR.TemplateNotTargetType

What it means

DataTemplate.SetTargetTypeInternal throws InvalidOperationException because DataTemplate does not support a direct TargetType; it exposes DataType instead. The parser calls SetTargetTypeInternal on Template subclasses, and DataTemplate forbids this route. Use the DataType property (often via x:Type markup) instead.

Solutions

  1. Set DataTemplate.DataType instead of TargetType (use x:Type in XAML: <DataTemplate DataType="{x:Type local:Customer}">)
  2. If you need a TargetType-style template, switch to ControlTemplate
  3. Use implicit data templates: DataType set to the CLR type applies automatically

Example fix

// before
var dt = new DataTemplate();
dt.TargetType = typeof(Customer); // not supported -> throws
// after
var dt = new DataTemplate();
dt.DataType = typeof(Customer);
Defensive patterns

Strategy: type-guard

Validate before calling

if (template is DataTemplate) template.DataType = targetType; else if (template is ControlTemplate ct) ct.TargetType = targetType;

Type guard

void SetTemplateType(FrameworkTemplate t, Type type) { switch (t) { case DataTemplate dt: dt.DataType = type; break; case ControlTemplate ct: ct.TargetType = type; break; default: throw new NotSupportedException(); } }

Try / catch

try { SetTargetTypeInternal(targetType); } catch (InvalidOperationException) { /* DataTemplate: use DataType instead */ }

Prevention

When it happens

Trigger: Programmatically or via the template-parsing pipeline attempting to set a TargetType directly on a DataTemplate object.

Common situations: Tooling/code-generation that treats all FrameworkTemplate subclasses uniformly; porting code from ControlTemplate (which does have TargetType) to DataTemplate; XAML that puts TargetType on a DataTemplate element.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DataTemplate.cs:142

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

        //
        //  TargetType for DataTemplate. 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 DataTemplate, this is not allowed.
        internal override void SetTargetTypeInternal(Type targetType)
        {
            throw new InvalidOperationException(SR.TemplateNotTargetType);
        }

        //
        //  DataType for DataTemplate. This is override is
        //  so FrameworkTemplate can see this property.
        //
        internal override object DataTypeInternal
        {
            get {  return DataType; }
        }

        //
        //  Collection of Triggers for a DataTemplate. This is
        //  override is so FrameworkTemplate can see this property.
        //
        internal override TriggerCollection TriggersInternal
        {
            get { return Triggers; }

View on GitHub (pinned to 81131a70a4)