dotnet/wpf · error · InvalidOperationException

SR.CannotHaveOverridesDefaultStyleInThemeStyle

Error message

SR.CannotHaveOverridesDefaultStyleInThemeStyle

What it means

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.

Solutions

  1. Remove any Setter or trigger that sets OverridesDefaultStyle from the theme/default style in generic.xaml.
  2. Set OverridesDefaultStyle="True" on the consuming element instance or in a separate app-level Style instead of the theme style.
  3. 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.

Example fix

<!-- before: generic.xaml default style -->
<Style TargetType="MyControl">
  <Setter Property="OverridesDefaultStyle" Value="True"/>
</Style>

<!-- after -->
<Style TargetType="MyControl">
  <!-- no OverridesDefaultStyle setter here -->
</Style>
Defensive patterns

Strategy: validation

Validate before calling

bool themeStyleInvalid(Style s) =>
    s.Setters.OfType<Setter>().Any(x => x.Property == FrameworkElement.OverridesDefaultStyleProperty);
// run before installing as DefaultStyleKey theme style

Type guard

bool isSafeThemeStyle(Style s) => !s.Setters.OfType<Setter>().Any(x => x.Property == FrameworkElement.OverridesDefaultStyleProperty);

Try / catch

try { control.ApplyTemplate(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("OverridesDefaultStyle")) {
    // remove the OverridesDefaultStyle setter from generic.xaml theme style
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/1f204963e696bbfb. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs:109

            ref Style               themeStyleCache)
        {
            Debug.Assert(fe != null || fce != null);

            if (newThemeStyle != null)
            {
                DependencyObject d = fe;
                if (d == null)
                {
                    d = fce;
                }
                newThemeStyle.CheckTargetType(d);
                newThemeStyle.Seal();

                // Check if the theme style has the OverridesDefaultStyle  property set on the target tag or any of its
                // visual triggers. It is an error to specify the OverridesDefaultStyle  in your own ThemeStyle.
                if (StyleHelper.IsSetOnContainer(FrameworkElement.OverridesDefaultStyleProperty, ref newThemeStyle.ContainerDependents, true))
                {
                    throw new InvalidOperationException(SR.CannotHaveOverridesDefaultStyleInThemeStyle);
                }
                // Check if the theme style has EventHandlers set on the target tag or int its setter collection.
                // We do not support EventHandlers in a ThemeStyle
                if (newThemeStyle.HasEventSetters)
                {
                    throw new InvalidOperationException(SR.CannotHaveEventHandlersInThemeStyle);
                }
            }

            themeStyleCache = newThemeStyle;

            Style style = null;

            if (fe != null)
            {
                if(ShouldGetValueFromStyle ( FrameworkElement.DefaultStyleKeyProperty ) )
                {
                    style = fe.Style;

View on GitHub (pinned to 81131a70a4)