{"record":{"id":"be6e0d8f81b1ea95","repo":"dotnet/wpf","slug":"sr-format-sr-cannothavepropertyintemplate-control","errorCode":null,"errorMessage":"SR.Format(SR.CannotHavePropertyInTemplate, Control.TemplateProperty.Name)","messagePattern":"SR\\.Format\\(SR\\.CannotHavePropertyInTemplate, Control\\.TemplateProperty\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":431,"sourceCode":"                ref frameworkTemplate.DataTriggersWithActions,\n                ref hasHandler );\n\n            frameworkTemplate.HasLoadedChangeHandler = hasHandler;\n\n            frameworkTemplate.SetResourceReferenceState();\n\n            // All done, seal self and call it a day.\n            isSealed = true;\n\n            // Remove thread affinity so it can be accessed across threads\n            frameworkTemplate.DetachFromDispatcher();\n\n            // Check if the template has the Template property set on the container via its visual triggers.\n            // It is an error to specify the TemplateProperty in your own Template.\n            if (StyleHelper.IsSetOnContainer(Control.TemplateProperty, ref containerDependents, true) ||\n                StyleHelper.IsSetOnContainer(ContentPresenter.TemplateProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, Control.TemplateProperty.Name));\n            }\n\n            // Check if the template has the Style property set on the container via its visual triggers.\n            // It is an error to specify the StyleProperty in your own Template.\n            if (StyleHelper.IsSetOnContainer(FrameworkElement.StyleProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.StyleProperty.Name));\n            }\n\n            // Check if the template has the DefaultStyleKey property set on the container via its visual triggers.\n            // It is an error to specify the DefaultStyleKeyProperty in your own Template.\n            if (StyleHelper.IsSetOnContainer(FrameworkElement.DefaultStyleKeyProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.DefaultStyleKeyProperty.Name));\n            }\n\n            // Check if the template has the OverridesDefaultStyle property set on the container via its visual triggers.\n            // It is an error to specify the OverridesDefaultStyleProperty in your own Template.","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L413-L449","documentation":"SealTemplate rejects a ControlTemplate/DataTemplate whose visual triggers (ContainerDependents) set the Template property (Control.TemplateProperty or ContentPresenter.TemplateProperty) on the container. A template cannot recursively set the Template property of the element it is a template for — that would be self-referential — so WPF throws InvalidOperationException when the template is sealed.","triggerScenarios":"Calling Template.Seal() (implicitly at first use) while the template contains a Trigger or setter targeting Control.TemplateProperty / ContentPresenter.TemplateProperty, e.g. <Setter Property=\"Template\"> inside a template's trigger or directly on the template's element tree.","commonSituations":"Copy-pasting a full <Style> body (including a Template setter) inside a ControlTemplate instead of a Style; nesting template declarations when converting a style into a template; tool-generated XAML that re-emits the Template setter inside the template.","solutions":["Delete any Setter/trigger that assigns the Template property from inside the template.","Keep Template assignments at the Style level: wrap the template in <Style><Setter Property=\"Template\"> instead.","If conditional templates are needed, use a TemplateSelector or swap templates via a Style trigger outside the template."],"exampleFix":"<!-- before: inside ControlTemplate.Triggers -->\n<ControlTemplate TargetType=\"Button\">\n  <ControlTemplate.Triggers>\n    <Trigger Property=\"IsMouseOver\" Value=\"True\">\n      <Setter Property=\"Template\">\n        <Setter.Value>\n          <ControlTemplate TargetType=\"Button\"/>\n        </Setter.Value>\n      </Setter>\n    </Trigger>\n  </ControlTemplate.Triggers>\n</ControlTemplate>\n\n<!-- after: change visual via style-level trigger, not Template setter -->\n<Trigger Property=\"IsMouseOver\" Value=\"True\">\n  <Setter Property=\"Background\" Value=\"LightBlue\"/>\n</Trigger>","handlingStrategy":"validation","validationCode":"bool templateSetsTemplate(ControlTemplate ct) =>\n    ct.Triggers.OfType<Trigger>().Any(t =>\n        t.Setters.OfType<Setter>().Any(s => s.Property == Control.TemplateProperty));","typeGuard":"bool isTemplateSelfContained(ControlTemplate ct) =>\n    !ct.Triggers.OfType<Trigger>().Any(t =>\n        t.Setters.OfType<Setter>().Any(s => s.Property == Control.TemplateProperty || s.Property == ContentPresenter.TemplateProperty));","tryCatchPattern":"try { element.ApplyTemplate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Template\")) {\n    // remove Template setters from inside the template\n}","preventionTips":["Never declare a Setter for Template/ContentPresenter.Template inside a ControlTemplate or DataTemplate.","Set Template only at the Style level; swap looks with Background/other-property triggers.","When refactoring a Style into a template, delete the Template setter — only the Setter.Value content becomes the template."],"tags":["wpf","xaml","templates","style"],"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"}