{"record":{"id":"c04659ccd5a2b362","repo":"dotnet/wpf","slug":"sr-cannothaveeventhandlersinthemestyle","errorCode":null,"errorMessage":"SR.CannotHaveEventHandlersInThemeStyle","messagePattern":"SR\\.CannotHaveEventHandlersInThemeStyle","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs","lineNumber":115,"sourceCode":"                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;\n                }\n            }\n            else\n            {\n                if(ShouldGetValueFromStyle ( FrameworkContentElement.DefaultStyleKeyProperty ) )\n                {","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/StyleHelper.cs#L97-L133","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Move EventSetter entries out of the theme style into an application/resource-dictionary-level Style.","Handle events in the control's code-behind (override OnClick/OnMouseDown etc.) instead of via style EventSetters.","Use EventTrigger animations in the template, or CommandBindings/RoutedCommand handlers on the control class."],"exampleFix":"<!-- before -->\n<Style TargetType=\"MyControl\">\n  <Setter Property=\"Template\" Value=\"{StaticResource MyControlTemplate}\"/>\n  <EventSetter Event=\"MouseLeftButtonDown\" Handler=\"OnMouseDown\"/>\n</Style>\n\n<!-- after: override in control class -->\nprotected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)\n{\n    // handle here instead of EventSetter\n}\n","handlingStrategy":"validation","validationCode":"bool themeStyleHasEventSetters(Style s) => s.Setters.OfType<EventSetter>().Any();\n// call before registering the style as a theme style","typeGuard":"bool isThemeStyleSafe(Style s) => !s.Setters.OfType<EventSetter>().Any();","tryCatchPattern":"try { element.ApplyTemplate(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"EventHandlers\") || ex.Message.Contains(\"EventSetter\")) {\n    // move EventSetters out of the theme style\n}","preventionTips":["Keep EventSetters out of themes/generic.xaml default styles entirely.","Handle control events via overridden On* methods or routed command handlers in the control class.","Audit shared resource dictionaries for EventSetter entries before reusing them as theme styles."],"tags":["wpf","xaml","themestyle","eventsetter"],"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"}