{"record":{"id":"a3f05336933de18e","repo":"dotnet/wpf","slug":"sr-templatenottargettype-datatemplate","errorCode":null,"errorMessage":"SR.TemplateNotTargetType","messagePattern":"SR\\.TemplateNotTargetType","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DataTemplate.cs","lineNumber":142,"sourceCode":"        //\n        //  Internal Properties\n        //\n        //-------------------------------------------------------------------\n\n        //\n        //  TargetType for DataTemplate. This is override is\n        //  so FrameworkTemplate can see this property.\n        //\n        internal override Type TargetTypeInternal\n        {\n            get {  return DefaultTargetType; }\n        }\n\n        // Subclasses must provide a way for the parser to directly set the\n        // target type.  For DataTemplate, this is not allowed.\n        internal override void SetTargetTypeInternal(Type targetType)\n        {\n            throw new InvalidOperationException(SR.TemplateNotTargetType);\n        }\n\n        //\n        //  DataType for DataTemplate. This is override is\n        //  so FrameworkTemplate can see this property.\n        //\n        internal override object DataTypeInternal\n        {\n            get {  return DataType; }\n        }\n\n        //\n        //  Collection of Triggers for a DataTemplate. This is\n        //  override is so FrameworkTemplate can see this property.\n        //\n        internal override TriggerCollection TriggersInternal\n        {\n            get { return Triggers; }","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DataTemplate.cs#L124-L160","documentation":"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.","triggerScenarios":"Programmatically or via the template-parsing pipeline attempting to set a TargetType directly on a DataTemplate object.","commonSituations":"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.","solutions":["Set DataTemplate.DataType instead of TargetType (use x:Type in XAML: <DataTemplate DataType=\"{x:Type local:Customer}\">)","If you need a TargetType-style template, switch to ControlTemplate","Use implicit data templates: DataType set to the CLR type applies automatically"],"exampleFix":"// before\nvar dt = new DataTemplate();\ndt.TargetType = typeof(Customer); // not supported -> throws\n// after\nvar dt = new DataTemplate();\ndt.DataType = typeof(Customer);","handlingStrategy":"type-guard","validationCode":"if (template is DataTemplate) template.DataType = targetType; else if (template is ControlTemplate ct) ct.TargetType = targetType;","typeGuard":"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(); } }","tryCatchPattern":"try { SetTargetTypeInternal(targetType); } catch (InvalidOperationException) { /* DataTemplate: use DataType instead */ }","preventionTips":["Remember: DataTemplate uses DataType, ControlTemplate uses TargetType","In XAML use DataType=\"{x:Type local:Foo}\" on DataTemplates","Avoid reflection/codegen that treats all templates uniformly"],"tags":["wpf","datatemplate","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}