{"record":{"id":"a0597de547bb0f7a","repo":"dotnet/wpf","slug":"sr-format-sr-cannothavepropertyintemplate-frameworkelement","errorCode":null,"errorMessage":"SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.StyleProperty.Name)","messagePattern":"SR\\.Format\\(SR\\.CannotHavePropertyInTemplate, FrameworkElement\\.StyleProperty\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":438,"sourceCode":"            // 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.\n            if (StyleHelper.IsSetOnContainer(FrameworkElement.OverridesDefaultStyleProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.OverridesDefaultStyleProperty.Name));\n            }\n\n            // Check if the template has the Name property set on the container via its visual triggers.\n            // It is an error to specify the Name in your own Template.","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L420-L456","documentation":"Thrown by ControlTemplate.SealTemplate (via StyleHelper) when a template's visual triggers set the Style property on the templated container. A template cannot define the Style of its own container, since Style and Template are mutually determined; WPF forbids it to prevent infinite template resolution. The exception is an InvalidOperationException raised while sealing the template, i.e. when the template is first applied.","triggerScenarios":"Defining a ControlTemplate (or FrameworkTemplate) whose Trigger, MultiTrigger, or TriggerBase setters contain a Setter with Property=\"Style\" targeting the container (TargetName omitted or set to the template root that maps to the container dependent property list). The error surfaces at template seal time — typically when the template is applied to a control.","commonSituations":"Copy-pasting a style trigger block into a template's <ControlTemplate.Triggers> and accidentally leaving a Setter for Style; tool-generated XAML that merges Style-level and template-level triggers; refactoring a Style so its Style setter drifted into the template triggers.","solutions":["Remove the Setter that targets the Style property from the template's Triggers; set Style via the Style itself or on the control instance instead.","If the intent was to restyle the template root element, give the root a named child (x:Name) and target the Setter with TargetName=\"...\", or set properties directly.","Replace the Style setter with individual property setters (e.g. Background, Foreground) inside the trigger.","Verify which template triggers set container-dependent properties before applying the template; StyleHelper.IsSetOnContainer flags Style, DefaultStyleKey, OverridesDefaultStyle, and Name."],"exampleFix":"<!-- before -->\n<ControlTemplate TargetType=\"Button\">\n  <ControlTemplate.Triggers>\n    <Trigger Property=\"IsMouseOver\" Value=\"True\">\n      <Setter Property=\"Style\" Value=\"{StaticResource HoverStyle}\"/>\n    </Trigger>\n  </ControlTemplate.Triggers>\n</ControlTemplate>\n\n<!-- after -->\n<ControlTemplate TargetType=\"Button\">\n  <ControlTemplate.Triggers>\n    <Trigger Property=\"IsMouseOver\" Value=\"True\">\n      <Setter Property=\"Background\" Value=\"LightBlue\"/>\n    </Trigger>\n  </ControlTemplate.Triggers>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"bool templateSetsContainerStyle(ControlTemplate t) =>\n    t.Triggers.Cast<TriggerBase>().SelectMany(tr => tr is MultiTrigger mt ? mt.Setters : tr.Setters.Cast<SetterBase>())\n     .OfType<Setter>().Any(s => s.Property == FrameworkElement.StyleProperty);","typeGuard":null,"tryCatchPattern":"try { element.Template = template; element.ApplyTemplate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Style\"))\n{\n    // log template name; strip offending container setters before retrying\n}","preventionTips":["Never put setters for Style, DefaultStyleKey, OverridesDefaultStyle, or Name inside ControlTemplate.Triggers","Set styles on the Style element or control instance, not the template","Lint XAML templates for container property setters before build","When generating templates in code, whitelist the properties setters may target"],"tags":["wpf","xaml","controltemplate","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}