{"record":{"id":"0ecf4f44efb3faed","repo":"dotnet/wpf","slug":"sr-templatenottargettype","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/Controls/ItemsPanelTemplate.cs","lineNumber":73,"sourceCode":"        //\n        //  Internal Properties\n        //\n        //-------------------------------------------------------------------\n\n        //\n        //  TargetType for ItemsPanelTemplate. 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 ItemsPanelTemplate, this is not allowed.\n        internal override void SetTargetTypeInternal(Type targetType)\n        {\n            throw new InvalidOperationException(SR.TemplateNotTargetType);\n        }\n\n        // Target type of ItemsPanelTemplate is ItemsPresenter\n        internal static Type DefaultTargetType\n        {\n            get { return typeof(ItemsPresenter); }\n        }\n\n        #endregion Internal Properties\n\n        #region Internal Methods\n\n        //-------------------------------------------------------------------\n        //\n        //  Internal Methods\n        //\n        //-------------------------------------------------------------------\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsPanelTemplate.cs#L55-L91","documentation":"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.","triggerScenarios":"Writing <ItemsPanelTemplate TargetType=\"...\"> in XAML, or calling SetTargetTypeInternal / a parser path that assigns TargetType on an ItemsPanelTemplate programmatically.","commonSituations":"Copy-pasting a ControlTemplate/DataTemplate declaration pattern (which do use TargetType) into an ItemsPanelTemplate; code generators emitting TargetType on all template types.","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."],"exampleFix":"// before\n<ItemsPanelTemplate TargetType=\"ItemsPresenter\">\n  <StackPanel Orientation=\"Horizontal\"/>\n</ItemsPanelTemplate>\n// after\n<ItemsPanelTemplate>\n  <StackPanel Orientation=\"Horizontal\"/>\n</ItemsPanelTemplate>","handlingStrategy":"validation","validationCode":"// XAML-time guard: never emit TargetType on ItemsPanelTemplate\nforeach (var el in xamlDoc.Descendants().Where(e => e.Name.LocalName == \"ItemsPanelTemplate\" && e.Attribute(\"TargetType\") != null))\n    el.Attribute(\"TargetType\").Remove();","typeGuard":"bool ItemsPanelTemplateHasNoTargetType(string xamlFragment) => !xamlFragment.Contains(\"ItemsPanelTemplate\") || !System.Text.RegularExpressions.Regex.IsMatch(xamlFragment, \"ItemsPanelTemplate[^>]*TargetType\");","tryCatchPattern":"try { ParseXaml(xaml); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"TargetType\")) { /* strip TargetType attr from ItemsPanelTemplate and retry */ }","preventionTips":["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."],"tags":["wpf","itemspaneltemplate","targettype","xaml"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}