{"record":{"id":"61e269214ac654f5","repo":"dotnet/wpf","slug":"sr-handlertypeillegal","errorCode":null,"errorMessage":"SR.HandlerTypeIllegal","messagePattern":"SR\\.HandlerTypeIllegal","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventHandlersStore.cs","lineNumber":147,"sourceCode":"        \n        #endregion ExternalAPI\n        \n        #region Operations\n\n        /// <summary>\n        ///     Adds a routed event handler for the given \n        ///     RoutedEvent to the store\n        /// </summary>\n        public void AddRoutedEventHandler(\n            RoutedEvent routedEvent,\n            Delegate handler,\n            bool handledEventsToo)\n        {\n            ArgumentNullException.ThrowIfNull(routedEvent);\n            ArgumentNullException.ThrowIfNull(handler);\n            if (!routedEvent.IsLegalHandler(handler))\n            {\n                throw new ArgumentException(SR.HandlerTypeIllegal);\n            }\n            \n            // Create a new RoutedEventHandler\n            RoutedEventHandlerInfo routedEventHandlerInfo = \n                new RoutedEventHandlerInfo(handler, handledEventsToo);\n\n            // Get the entry corresponding to the given RoutedEvent\n            FrugalObjectList<RoutedEventHandlerInfo> handlers = (FrugalObjectList<RoutedEventHandlerInfo>)this[routedEvent];\n            if (handlers == null)\n            {\n                _entries[routedEvent.GlobalIndex] = handlers = new FrugalObjectList<RoutedEventHandlerInfo>(1);\n            }\n\n            // Add the RoutedEventHandlerInfo to the list\n            handlers.Add(routedEventHandlerInfo);\n        }\n\n        /// <summary>","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventHandlersStore.cs#L129-L165","documentation":"EventHandlersStore.AddRoutedEventHandler validates that the delegate's signature matches the handler type declared for the routed event via RoutedEvent.IsLegalHandler. When the delegate type is incompatible with routedEvent.HandlerType it throws ArgumentException with SR.HandlerTypeIllegal. Called by AddDelegateToFireTrigger when wiring trigger handlers.","triggerScenarios":"Calling AddRoutedEventHandler with a Delegate whose parameter/return types do not match routedEvent.HandlerType — e.g. passing a RoutedEventHandler to an event declared with KeyRoutedEventHandler, or a plain EventHandler for a routed event.","commonSituations":"Copy-pasting handler wiring code between different events; generic trigger frameworks adding delegates of the wrong type; API changes where an event's handler type was tightened.","solutions":["Pass a delegate of exactly routedEvent.HandlerType (cast/create a matching delegate)","Check routedEvent.HandlerType before creating the delegate","Use the typed CLR event accessor (e.g. element.MouseDown += ...) instead of store-level APIs"],"exampleFix":"// before\nstore.AddRoutedEventHandler(Keyboard.KeyDownEvent, new RoutedEventHandler(OnKeyDown), false);\n// after\nstore.AddRoutedEventHandler(Keyboard.KeyDownEvent, new KeyEventHandler(OnKeyDown), false);","handlingStrategy":"validation","validationCode":"if (routedEvent.IsLegalHandler(handler)) { store.AddRoutedEventHandler(routedEvent, handler, handledEventsToo); }","typeGuard":"bool IsLegalFor(RoutedEvent e, Delegate h) => e != null && h != null && e.IsLegalHandler(h);","tryCatchPattern":"try { store.AddRoutedEventHandler(evt, handler, false); } catch (ArgumentException) { /* wrong delegate type for evt.HandlerType */ }","preventionTips":["Match the delegate type to routedEvent.HandlerType exactly","Prefer typed CLR event accessors over EventHandlersStore internals","Never pass lambdas as plain Action/EventHandler delegates to routed-event plumbing"],"tags":["wpf","routed-events","delegate","argument"],"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"}