{"record":{"id":"7b19045042009ddc","repo":"dotnet/wpf","slug":"sr-mismatched-routedevent","errorCode":null,"errorMessage":"SR.Mismatched_RoutedEvent","messagePattern":"SR\\.Mismatched_RoutedEvent","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventRoute.cs","lineNumber":133,"sourceCode":"        internal void ReInvokeHandlers(object source, RoutedEventArgs args)\n        {\n            InvokeHandlersImpl(source, args, true);\n        }\n\n        private void InvokeHandlersImpl(object source, RoutedEventArgs args, bool reRaised)\n        {\n            ArgumentNullException.ThrowIfNull(source);\n\n            ArgumentNullException.ThrowIfNull(args);\n\n            if (args.Source == null)\n            {\n                throw new ArgumentException(SR.SourceNotSet); \n            }\n\n            if (args.RoutedEvent != _routedEvent)\n            {\n                throw new ArgumentException(SR.Mismatched_RoutedEvent);\n            }\n\n            // Check RoutingStrategy to know the order of invocation\n            if (args.RoutedEvent.RoutingStrategy == RoutingStrategy.Bubble ||\n                args.RoutedEvent.RoutingStrategy == RoutingStrategy.Direct)\n            {\n                int endSourceChangeIndex = 0;\n                \n                // If the RoutingStrategy of the associated is \n                // Bubble the handlers for the last target \n                // added are the last ones invoked\n                // Invoke class listeners\n                for (int i=0; i<_routeItemList.Count; i++)\n                {\n                    // Query for new source only if we are \n                    // past the range of the previous source change\n                    if (i >= endSourceChangeIndex)\n                    {","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventRoute.cs#L115-L151","documentation":"EventRoute.InvokeHandlersImpl verifies that the RoutedEventArgs passed matches the route: args.RoutedEvent must equal the route's _routedEvent. A mismatch throws ArgumentException with SR.Mismatched_RoutedEvent. This prevents raising one event's args along another event's route.","triggerScenarios":"Building an EventRoute for one RoutedEvent but invoking it with RoutedEventArgs whose RoutedEvent property is a different event — common in custom RaiseEvent implementations that reuse a route or a cached args object across events.","commonSituations":"Custom framework code caching EventRoute instances keyed incorrectly; reusing RoutedEventArgs instances for multiple event types; third-party eventing helpers constructing routes generically.","solutions":["Ensure args.RoutedEvent equals the RoutedEvent the route was built for","Create a fresh RoutedEventArgs (with the correct RoutedEvent) per route invocation","If reusing routes, key the cache by RoutedEvent and verify before invoking"],"exampleFix":"// before\nEventRoute route = new EventRoute(OtherEvent);\nroute.InvokeHandlers(this, new RoutedEventArgs(MyEvent)); // mismatch\n// after\nEventRoute route = new EventRoute(MyEvent);\nroute.InvokeHandlers(this, new RoutedEventArgs(MyEvent));","handlingStrategy":"validation","validationCode":"if (args != null && ReferenceEquals(args.RoutedEvent, route.RoutedEvent)) { route.InvokeHandlers(this, args); }","typeGuard":"bool ArgsMatchRoute(EventRoute r, RoutedEventArgs a) => a != null && ReferenceEquals(a.RoutedEvent, r.RoutedEvent);","tryCatchPattern":"try { route.InvokeHandlers(this, args); } catch (ArgumentException) { /* args.RoutedEvent != route._routedEvent */ }","preventionTips":["Create one RoutedEventArgs per RoutedEvent; never share across events","Key any EventRoute cache by RoutedEvent","Assert ReferenceEquals(args.RoutedEvent, route.RoutedEvent) in debug builds"],"tags":["wpf","routed-events","mismatch","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"}