{"record":{"id":"1f204963e696bbfb","repo":"dotnet/wpf","slug":"sr-cannothaveoverridesdefaultstyleinthemestyle","errorCode":null,"errorMessage":"SR.CannotHaveOverridesDefaultStyleInThemeStyle","messagePattern":"SR\\.CannotHaveOverridesDefaultStyleInThemeStyle","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":109,"sourceCode":"            ref Style               themeStyleCache)\n        {\n            Debug.Assert(fe != null || fce != null);\n\n            if (newThemeStyle != null)\n            {\n                DependencyObject d = fe;\n                if (d == null)\n                {\n                    d = fce;\n                }\n                newThemeStyle.CheckTargetType(d);\n                newThemeStyle.Seal();\n\n                // Check if the theme style has the OverridesDefaultStyle  property set on the target tag or any of its\n                // visual triggers. It is an error to specify the OverridesDefaultStyle  in your own ThemeStyle.\n                if (StyleHelper.IsSetOnContainer(FrameworkElement.OverridesDefaultStyleProperty, ref newThemeStyle.ContainerDependents, true))\n                {\n                    throw new InvalidOperationException(SR.CannotHaveOverridesDefaultStyleInThemeStyle);\n                }\n                // Check if the theme style has EventHandlers set on the target tag or int its setter collection.\n                // We do not support EventHandlers in a ThemeStyle\n                if (newThemeStyle.HasEventSetters)\n                {\n                    throw new InvalidOperationException(SR.CannotHaveEventHandlersInThemeStyle);\n                }\n            }\n\n            themeStyleCache = newThemeStyle;\n\n            Style style = null;\n\n            if (fe != null)\n            {\n                if(ShouldGetValueFromStyle ( FrameworkElement.DefaultStyleKeyProperty ) )\n                {\n                    style = fe.Style;","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L91-L127","documentation":"A ThemeStyle (the default style looked up via DefaultStyleKey / ThemeStyleKey) must not set the OverridesDefaultStyle property on the target element or in its visual triggers. StyleHelper.UpdateThemeStyleCache checks this while sealing the new theme style and throws InvalidOperationException because letting a theme style override the default-style mechanism would create a circular, self-defeating definition.","triggerScenarios":"Sealing a theme style whose ContainerDependents include FrameworkElement.OverridesDefaultStyleProperty — i.e. <Setter Property=\"OverridesDefaultStyle\" Value=\"True\"/> or a trigger setter on it inside the style used as the DefaultStyleKey resource.","commonSituations":"Custom control authors copying a normal Style (which legally may set OverridesDefaultStyle) into their themes/generic.xaml default style; merging resource dictionaries so a style becomes the theme style accidentally.","solutions":["Remove any Setter or trigger that sets OverridesDefaultStyle from the theme/default style in generic.xaml.","Set OverridesDefaultStyle=\"True\" on the consuming element instance or in a separate app-level Style instead of the theme style.","If the intent is to replace a control's default look, derive a new style keyed to the control type in app resources rather than editing the theme style."],"exampleFix":"<!-- before: generic.xaml default style -->\n<Style TargetType=\"MyControl\">\n  <Setter Property=\"OverridesDefaultStyle\" Value=\"True\"/>\n</Style>\n\n<!-- after -->\n<Style TargetType=\"MyControl\">\n  <!-- no OverridesDefaultStyle setter here -->\n</Style>","handlingStrategy":"validation","validationCode":"bool themeStyleInvalid(Style s) =>\n    s.Setters.OfType<Setter>().Any(x => x.Property == FrameworkElement.OverridesDefaultStyleProperty);\n// run before installing as DefaultStyleKey theme style","typeGuard":"bool isSafeThemeStyle(Style s) => !s.Setters.OfType<Setter>().Any(x => x.Property == FrameworkElement.OverridesDefaultStyleProperty);","tryCatchPattern":"try { control.ApplyTemplate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"OverridesDefaultStyle\")) {\n    // remove the OverridesDefaultStyle setter from generic.xaml theme style\n}","preventionTips":["Never set OverridesDefaultStyle inside the generic.xaml default style of a control.","Apply OverridesDefaultStyle on consuming instances or derived app-level styles only.","When copying styles into generic.xaml, strip instance-level overrides first."],"tags":["wpf","xaml","themestyle","defaultstyle"],"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"}