{"record":{"id":"0d2f03728612428c","repo":"dotnet/wpf","slug":"sr-format-sr-systemresourcefortypeisnotstyle-themestylekey","errorCode":null,"errorMessage":"SR.Format(SR.SystemResourceForTypeIsNotStyle, themeStyleKey)","messagePattern":"SR\\.Format\\(SR\\.SystemResourceForTypeIsNotStyle, themeStyleKey\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":228,"sourceCode":"                else\n                {\n                    // Regular lookup based on the DefaultStyleKey. Involves locking and Hashtable lookup\n                    styleLookup = SystemResources.FindResourceInternal(themeStyleKey);\n                }\n\n                if( styleLookup != null )\n                {\n                    if( styleLookup is Style )\n                    {\n                        // We have found an applicable Style in system resources\n                        //  let's us use that as second stop to find property values.\n                        newThemeStyle = (Style)styleLookup;\n                    }\n                    else\n                    {\n                        // We found something keyed to the ThemeStyleKey, but it's not\n                        //  a style.  This is a problem, throw an exception here.\n                        throw new InvalidOperationException(SR.Format(\n                            SR.SystemResourceForTypeIsNotStyle, themeStyleKey));\n                    }\n                }\n\n                if (newThemeStyle == null)\n                {\n                    // No style in system resources, try to retrieve the default\n                    //  style for the target type.\n                    Type themeStyleTypeKey = themeStyleKey as Type;\n                    if (themeStyleTypeKey != null)\n                    {\n                        PropertyMetadata styleMetadata =\n                            FrameworkElement.StyleProperty.GetMetadata(themeStyleTypeKey);\n\n                        if( styleMetadata != null )\n                        {\n                            // Have a metadata object, get the default style (if any)\n                            newThemeStyle = styleMetadata.DefaultValue as Style;","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L210-L246","documentation":"GetThemeStyle looks up the resource dictionary using the control's ThemeStyleKey (usually the DefaultStyleKey type). If an entry with that key exists but is not a Style, WPF cannot use it as the theme style and throws InvalidOperationException naming the offending key. This is an authoring/resource-dictionary type error: something other than a Style was registered under a control's default style key.","triggerScenarios":"A resource such as <DataTemplate x:Key=\"{x:Type MyControl}\">, a Brush, or a FrameworkElement is placed in themes/generic.xaml (or an implicit-key dictionary) under the type used as DefaultStyleKey, then an instance of the control is created and GetThemeStyle resolves it.","commonSituations":"Copy-paste in generic.xaml where a Template or DataTemplate was given x:Key=\"{x:Type LocalControl}\" instead of x:Key=\"LocalControlTemplate\"; DynamicResource confusion putting data under type keys; merging dictionaries where keys collide.","solutions":["Find the resource keyed with x:Key=\"{x:Type YourControl}\" (or the DefaultStyleKey type) and change it to a <Style TargetType=...> with that key.","Give non-Style resources (templates, brushes) their own string keys and reference them from within the style/template.","Verify DefaultStyleKeyProperty.OverrideMetadata points at a type for which a valid Style resource exists in generic.xaml."],"exampleFix":"<!-- before -->\n<DataTemplate x:Key=\"{x:Type local:MyControl}\">\n  <TextBlock Text=\"hi\"/>\n</DataTemplate>\n\n<!-- after -->\n<Style TargetType=\"local:MyControl\" x:Key=\"{x:Type local:MyControl}\">\n  <Setter Property=\"Template\">\n    <Setter.Value>\n      <ControlTemplate TargetType=\"local:MyControl\">\n        <TextBlock Text=\"hi\"/>\n      </ControlTemplate>\n    </Setter.Value>\n  </Setter>\n</Style>","handlingStrategy":"validation","validationCode":"object res = Application.Current.TryFindResource(defaultStyleKey);\nif (res != null && !(res is Style))\n    throw new InvalidOperationException($\"Resource keyed to {defaultStyleKey} is {res.GetType().Name}, expected Style.\");","typeGuard":"bool isThemeStyleResource(object res) => res is Style;\n// check the resource found under the DefaultStyleKey before instantiating the control","tryCatchPattern":"try { var ctrl = new MyControl(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not\") && ex.Message.Contains(\"Style\")) {\n    // inspect generic.xaml for the wrong resource under {x:Type MyControl}\n}","preventionTips":["Reserve x:Key=\"{x:Type YourControl}\" exclusively for Style resources in generic.xaml.","Give templates, brushes, and data separate string keys.","After merging resource dictionaries, verify no non-Style resource uses a type key of a control."],"tags":["wpf","xaml","resources","themestyle"],"backgroundTag":"type-mismatch","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"}