{"record":{"id":"5bc8a6ac4dae2ec4","repo":"dotnet/wpf","slug":"sr-mismatched-routedevent-uielement","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/UIElement.cs","lineNumber":1649,"sourceCode":"        {\n//             VerifyAccess();\n            ContextLayoutManager.From(Dispatcher).UpdateLayout();\n        }\n\n        internal static void BuildRouteHelper(DependencyObject e, EventRoute route, RoutedEventArgs args)\n        {\n            ArgumentNullException.ThrowIfNull(route);\n\n            ArgumentNullException.ThrowIfNull(args);\n\n            if (args.Source == null)\n            {\n                throw new ArgumentException(SR.SourceNotSet);\n            }\n\n            if (args.RoutedEvent != route.RoutedEvent)\n            {\n                throw new ArgumentException(SR.Mismatched_RoutedEvent);\n            }\n\n            // Route via visual tree\n            if (args.RoutedEvent.RoutingStrategy == RoutingStrategy.Direct)\n            {\n                UIElement uiElement = e as UIElement;\n                ContentElement contentElement = null;\n                UIElement3D uiElement3D = null;\n\n                if (uiElement == null)\n                {\n                    contentElement = e as ContentElement;\n\n                    if (contentElement == null)\n                    {\n                        uiElement3D = e as UIElement3D;\n                    }\n                }","sourceCodeStart":1631,"sourceCodeEnd":1667,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs#L1631-L1667","documentation":"The RoutedEventArgs being raised does not match the RoutedEvent of the route it is being sent through. UIElement's raise-impl validates args.RoutedEvent == route.RoutedEvent and throws ArgumentException(SR.Mismatched_RoutedEvent) otherwise. This guards against routing an event instance through an unrelated event definition.","triggerScenarios":"Reusing a cached RoutedEventArgs (or args from another event) with a different RoutedEvent; passing the wrong RoutedEvent to RaiseEvent; custom routing code pairing a route with foreign args.","commonSituations":"Pooled/shared RoutedEventArgs instances after refactoring event definitions; copy-pasted RaiseEvent calls where the event field was renamed; framework code routing through EventRoute manually.","solutions":["Ensure args.RoutedEvent equals the RoutedEvent passed/used for raising; create a new RoutedEventArgs for the correct event","Stop sharing RoutedEventArgs instances across different events","Fix the RoutedEvent reference (copy-paste/rename bug) in the RaiseEvent call"],"exampleFix":"// before\nvar args = new RoutedEventArgs(OtherEvent);\nelement.RaiseEvent(MyEvent, args); // mismatch\n// after\nvar args = new RoutedEventArgs(MyEvent);\nelement.RaiseEvent(MyEvent, args);","handlingStrategy":"validation","validationCode":"if (args.RoutedEvent != route.RoutedEvent) throw new InvalidOperationException(\"RoutedEvent mismatch before RaiseEvent\");\nelement.RaiseEvent(args);","typeGuard":"static bool MatchesRoute(RoutedEvent routeEvent, RoutedEventArgs args) => args?.RoutedEvent == routeEvent;","tryCatchPattern":"try { element.RaiseEvent(args); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"RoutedEvent\")) { log.Warn(\"Event/args mismatch\", ex); }","preventionTips":["Do not pool or share RoutedEventArgs instances across different events","Keep one RaiseEvent call site per RoutedEvent to avoid copy-paste mismatches","After renaming an event, audit its RaiseEvent callers"],"tags":["wpf","routed-events","argument-validation","type-mismatch"],"backgroundTag":"invalid-argument-value","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"}