{"record":{"id":"c31e11acda4ce849","repo":"dotnet/wpf","slug":"sr-format-sr-cannothavepropertyintemplate-frameworkelement-c31e11","errorCode":null,"errorMessage":"SR.Format(SR.CannotHavePropertyInTemplate, FrameworkElement.OverridesDefaultStyleProperty.Name)","messagePattern":"SR\\.Format\\(SR\\.CannotHavePropertyInTemplate, FrameworkElement\\.OverridesDefaultStyleProperty\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":452,"sourceCode":"            // 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\n\n        //\n        //  All table datastructures read-lock-free/write-lock\n        //  UpdateTables writes the datastructures, locks set by callers\n        //\n        //  This method\n        //  1. Adds a ChildValueLookup entry to the given ChildRecord.","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L434-L470","documentation":"Thrown by ControlTemplate.SealTemplate when the template's visual triggers set OverridesDefaultStyle on the templated container. OverridesDefaultStyle controls whether theme styles apply to the container; letting a template toggle it on its own container is disallowed, and WPF throws InvalidOperationException during sealing. The message names the property (OverridesDefaultStyle).","triggerScenarios":"A Setter with Property=\"OverridesDefaultStyle\" inside ControlTemplate.Triggers (or any template visual trigger affecting the container). Raised by StyleHelper.IsSetOnContainer when the template is sealed.","commonSituations":"XAML copied from a Style-level trigger (where OverridesDefaultStyle setters are legal on inner elements) pasted into template triggers; attempts to suppress theme styling for one trigger state; migration of WPF 3.x-era markup into stricter template parsing paths.","solutions":["Remove the OverridesDefaultStyle setter from the template triggers.","Set OverridesDefaultStyle=\"True\" on the Style that applies the template, or on a named inner element via TargetName.","Control theme-style suppression with BasedOn composition of Styles instead of template triggers.","Pre-validate template XAML for the forbidden container properties Style/DefaultStyleKey/OverridesDefaultStyle/Name."],"exampleFix":"<!-- before -->\n<ControlTemplate.Triggers>\n  <Trigger Property=\"IsEnabled\" Value=\"False\">\n    <Setter Property=\"OverridesDefaultStyle\" Value=\"True\"/>\n  </Trigger>\n</ControlTemplate.Triggers>\n\n<!-- after -->\n<Style TargetType=\"Button\" BasedOn=\"{StaticResource {x:Type Button}}\">\n  <Setter Property=\"OverridesDefaultStyle\" Value=\"True\"/>\n  <Setter Property=\"Template\" Value=\"{StaticResource MyTemplate}\"/>\n</Style>","handlingStrategy":"validation","validationCode":"bool templateSetsOverridesDefaultStyle(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.OverridesDefaultStyleProperty);","typeGuard":null,"tryCatchPattern":"try { element.Template = t; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"OverridesDefaultStyle\"))\n{\n    // move the setting to the Style level and retry\n}","preventionTips":["Set OverridesDefaultStyle at Style level or on named inner elements, not in template triggers","Use BasedOn to compose theme styles instead of toggling OverridesDefaultStyle","Lint templates for the four forbidden container properties","When copying trigger XAML between Style and Template scopes, strip container-property 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"}