dotnet/wpf · error · InvalidOperationException

SR.CannotHaveEventHandlersInThemeStyle

Error message

SR.CannotHaveEventHandlersInThemeStyle

What it means

EventSetters (the legacy EventHandlers feature) are not supported inside a ThemeStyle. During UpdateThemeStyleCache, if the sealed newThemeStyle.HasEventSetters is true, WPF throws InvalidOperationException because event handler wiring is not permitted in the default/theme style of a control.

Solutions

  1. Move EventSetter entries out of the theme style into an application/resource-dictionary-level Style.
  2. Handle events in the control's code-behind (override OnClick/OnMouseDown etc.) instead of via style EventSetters.
  3. Use EventTrigger animations in the template, or CommandBindings/RoutedCommand handlers on the control class.

Example fix

<!-- before -->
<Style TargetType="MyControl">
  <Setter Property="Template" Value="{StaticResource MyControlTemplate}"/>
  <EventSetter Event="MouseLeftButtonDown" Handler="OnMouseDown"/>
</Style>

<!-- after: override in control class -->
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
    // handle here instead of EventSetter
}
Defensive patterns

Strategy: validation

Validate before calling

bool themeStyleHasEventSetters(Style s) => s.Setters.OfType<EventSetter>().Any();
// call before registering the style as a theme style

Type guard

bool isThemeStyleSafe(Style s) => !s.Setters.OfType<EventSetter>().Any();

Try / catch

try { element.ApplyTemplate(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("EventHandlers") || ex.Message.Contains("EventSetter")) {
    // move EventSetters out of the theme style
}

Prevention

When it happens

Trigger: Sealing a theme style that contains EventSetter entries (e.g. <EventSetter Event="Click" Handler="OnClick"/>) in its Setters collection, detected when the style is installed as a control's DefaultStyleKey-resolved style.

Common situations: Reusing an application-level style with EventSetters as a control theme style; migrating old code that used the pre-release 'EventHandlers' syntax into a theme style; copy-pasting styles between generic.xaml and app resources.

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/c04659ccd5a2b362. Report an issue: GitHub.

Appendix: source

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

                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;
                }
            }
            else
            {
                if(ShouldGetValueFromStyle ( FrameworkContentElement.DefaultStyleKeyProperty ) )
                {

View on GitHub (pinned to 81131a70a4)