dotnet/wpf · error · InvalidOperationException
SR.Format(SR.SystemResourceForTypeIsNotStyle, themeStyleKey)
Error message
SR.Format(SR.SystemResourceForTypeIsNotStyle, themeStyleKey)
What it means
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.
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.
Example fix
<!-- before -->
<DataTemplate x:Key="{x:Type local:MyControl}">
<TextBlock Text="hi"/>
</DataTemplate>
<!-- after -->
<Style TargetType="local:MyControl" x:Key="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyControl">
<TextBlock Text="hi"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> Defensive patterns
Strategy: validation
Validate before calling
object res = Application.Current.TryFindResource(defaultStyleKey);
if (res != null && !(res is Style))
throw new InvalidOperationException($"Resource keyed to {defaultStyleKey} is {res.GetType().Name}, expected Style."); Type guard
bool isThemeStyleResource(object res) => res is Style; // check the resource found under the DefaultStyleKey before instantiating the control
Try / catch
try { var ctrl = new MyControl(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not") && ex.Message.Contains("Style")) {
// inspect generic.xaml for the wrong resource under {x:Type MyControl}
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.CannotHaveEventHandlersInThemeStyle
- SR.CannotHaveOverridesDefaultStyleInThemeStyle
- Animation_ChildMustBeKeyFrame
- Animation_ChildMustBeKeyFrame
- Animation_NoTextChildren
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0d2f03728612428c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs:228
else
{
// Regular lookup based on the DefaultStyleKey. Involves locking and Hashtable lookup
styleLookup = SystemResources.FindResourceInternal(themeStyleKey);
}
if( styleLookup != null )
{
if( styleLookup is Style )
{
// We have found an applicable Style in system resources
// let's us use that as second stop to find property values.
newThemeStyle = (Style)styleLookup;
}
else
{
// We found something keyed to the ThemeStyleKey, but it's not
// a style. This is a problem, throw an exception here.
throw new InvalidOperationException(SR.Format(
SR.SystemResourceForTypeIsNotStyle, themeStyleKey));
}
}
if (newThemeStyle == null)
{
// No style in system resources, try to retrieve the default
// style for the target type.
Type themeStyleTypeKey = themeStyleKey as Type;
if (themeStyleTypeKey != null)
{
PropertyMetadata styleMetadata =
FrameworkElement.StyleProperty.GetMetadata(themeStyleTypeKey);
if( styleMetadata != null )
{
// Have a metadata object, get the default style (if any)
newThemeStyle = styleMetadata.DefaultValue as Style;View on GitHub (pinned to 81131a70a4)