{"record":{"id":"8cad52f640f14337","repo":"dotnet/wpf","slug":"sr-format-sr-cannothavepropertyintemplate-frameworkelement-8cad52","errorCode":null,"errorMessage":"SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.DefaultStyleKeyProperty.Name)","messagePattern":"SR\\.Format\\(SR\\.CannotHavePropertyInTemplate, FrameworkElement\\.DefaultStyleKeyProperty\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":445,"sourceCode":"            // 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.\n            if (StyleHelper.IsSetOnContainer(FrameworkElement.NameProperty, ref containerDependents, true))\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.NameProperty.Name));\n            }\n        }\n\n","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L427-L463","documentation":"Thrown by ControlTemplate.SealTemplate when the template's visual triggers set DefaultStyleKey on the templated container. DefaultStyleKey decides which theme style a control looks up; setting it from within that very template is a circular dependency, so WPF rejects it with InvalidOperationException at seal time. The message names the offending property (DefaultStyleKey).","triggerScenarios":"A ControlTemplate.Triggers section containing a Setter with Property=\"DefaultStyleKey\". Detected by StyleHelper.IsSetOnContainer during SealTemplate, i.e. the first time the template is sealed/applied.","commonSituations":"Custom control authors copying template trigger XAML from another control that manipulated theme keys; metadata-generated or machine-converted XAML carrying a DefaultStyleKey setter into template triggers; attempted runtime theming by swapping DefaultStyleKey inside a template.","solutions":["Remove the DefaultStyleKey setter from the template's triggers.","Set DefaultStyleKey via DefaultStyleKeyProperty.OverrideMetadata in the control's static constructor instead.","If per-state key switching is needed, swap the Template or Style on the control, not DefaultStyleKey inside a template.","Audit templates with a check for the four forbidden container properties (Style, DefaultStyleKey, OverridesDefaultStyle, Name) before applying them."],"exampleFix":"// before (template trigger)\n<Setter Property=\"DefaultStyleKey\" Value=\"{x:Static local:MyControl.AlternateKey}\"/>\n\n// after (control static constructor)\nstatic MyControl()\n{\n    DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl),\n        new FrameworkPropertyMetadata(typeof(MyControl)));\n}","handlingStrategy":"validation","validationCode":"bool templateSetsDefaultStyleKey(ControlTemplate t) =>\n    t.Triggers.Cast<TriggerBase>()\n     .SelectMany(tr => tr is MultiTrigger mt ? mt.Setters : tr.Setters.Cast<SetterBase>())\n     .OfType<Setter>().Any(s => s.Property == FrameworkElement.DefaultStyleKeyProperty);","typeGuard":null,"tryCatchPattern":"try { element.ApplyTemplate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DefaultStyleKey\"))\n{\n    // strip the DefaultStyleKey setter and reapply\n}","preventionTips":["Override DefaultStyleKey only in the control's static constructor via OverrideMetadata","Do not theme-switch by setting DefaultStyleKey from within templates","Keep theme key management in Style/metadata, never in template triggers","Review machine-converted XAML for leaked DefaultStyleKey setters"],"tags":["wpf","xaml","controltemplate","theming"],"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"}