{"record":{"id":"0229ba304639e0c0","repo":"dotnet/wpf","slug":"sr-handlertypeillegal-eventsetter","errorCode":null,"errorMessage":"SR.HandlerTypeIllegal","messagePattern":"SR\\.HandlerTypeIllegal","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventSetter.cs","lineNumber":103,"sourceCode":"        //\n        //  Do the error checking that we can only do once all of the properties have been\n        //  set, then call up to base.\n        //\n        \n        internal override void Seal()\n        {\n\n            if (_event == null)\n            {\n                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, \"EventSetter.Event\"));\n            }\n            if (_handler == null)\n            {\n                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, \"EventSetter.Handler\"));\n            }\n            if (_handler.GetType() != _event.HandlerType)\n            {\n                throw new ArgumentException(SR.HandlerTypeIllegal);\n            }\n\n            base.Seal();\n            \n        }\n\n\n        private RoutedEvent    _event;\n        private Delegate         _handler;\n        private bool             _handledEventsToo;\n    }\n    \n}\n\n","sourceCodeStart":85,"sourceCodeEnd":118,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventSetter.cs#L85-L118","documentation":"EventSetter.Seal checks that the Handler delegate's type matches the EventHandlerType of the routed Event. A mismatch throws ArgumentException with HandlerTypeIllegal, preventing a wrongly-typed delegate from being attached to the event.","triggerScenarios":"Setting Event to a routed event whose handler type is, say, MouseButtonEventHandler, but supplying a RoutedEventHandler (or vice versa), then sealing the style.","commonSituations":"Copy-pasting EventSetter definitions between events with different signatures; changing the Event attribute in XAML without updating the Handler method signature.","solutions":["Match the handler signature to the event: use MouseButtonEventHandler for MouseLeftButtonDown, etc.","Change the Event to one that accepts the handler type you already have","Wrap the existing method: Handler = new MouseButtonEventHandler((s,e) => OnClick(s, (RoutedEventArgs)e))"],"exampleFix":"// before\nvar es = new EventSetter {\n  Event = Mouse.MouseDownEvent, // needs MouseButtonEventHandler\n  Handler = new RoutedEventHandler(OnClick) };\n// after\nvar es = new EventSetter {\n  Event = Mouse.MouseDownEvent,\n  Handler = new MouseButtonEventHandler(OnClick) };","handlingStrategy":"validation","validationCode":"if (eventSetter.Handler != null && eventSetter.Event != null &&\n    eventSetter.Handler.GetType() != eventSetter.Event.HandlerType)\n    throw new InvalidOperationException($\"Handler must be {eventSetter.Event.HandlerType.Name}\");","typeGuard":"bool HandlerMatches(EventSetter s) => s.Handler.GetType() == s.Event.HandlerType;","tryCatchPattern":"try { style.Seal(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"handler\")) { recreateSetterWithCorrectDelegate(); }","preventionTips":["Match delegate types to each event's HandlerType","Avoid copy-pasting EventSetter definitions between differently-typed events"],"tags":["wpf","style","eventsetter","delegate","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}