dotnet/wpf · error · ArgumentException

SR.Mismatched_RoutedEvent

Error message

SR.Mismatched_RoutedEvent

What it means

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.

Solutions

  1. Ensure args.RoutedEvent equals the RoutedEvent the route was built for
  2. Create a fresh RoutedEventArgs (with the correct RoutedEvent) per route invocation
  3. If reusing routes, key the cache by RoutedEvent and verify before invoking

Example fix

// before
EventRoute route = new EventRoute(OtherEvent);
route.InvokeHandlers(this, new RoutedEventArgs(MyEvent)); // mismatch
// after
EventRoute route = new EventRoute(MyEvent);
route.InvokeHandlers(this, new RoutedEventArgs(MyEvent));
Defensive patterns

Strategy: validation

Validate before calling

if (args != null && ReferenceEquals(args.RoutedEvent, route.RoutedEvent)) { route.InvokeHandlers(this, args); }

Type guard

bool ArgsMatchRoute(EventRoute r, RoutedEventArgs a) => a != null && ReferenceEquals(a.RoutedEvent, r.RoutedEvent);

Try / catch

try { route.InvokeHandlers(this, args); } catch (ArgumentException) { /* args.RoutedEvent != route._routedEvent */ }

Prevention

When it happens

Trigger: 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.

Common situations: Custom framework code caching EventRoute instances keyed incorrectly; reusing RoutedEventArgs instances for multiple event types; third-party eventing helpers constructing routes generically.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7b19045042009ddc. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventRoute.cs:133

        internal void ReInvokeHandlers(object source, RoutedEventArgs args)
        {
            InvokeHandlersImpl(source, args, true);
        }

        private void InvokeHandlersImpl(object source, RoutedEventArgs args, bool reRaised)
        {
            ArgumentNullException.ThrowIfNull(source);

            ArgumentNullException.ThrowIfNull(args);

            if (args.Source == null)
            {
                throw new ArgumentException(SR.SourceNotSet); 
            }

            if (args.RoutedEvent != _routedEvent)
            {
                throw new ArgumentException(SR.Mismatched_RoutedEvent);
            }

            // Check RoutingStrategy to know the order of invocation
            if (args.RoutedEvent.RoutingStrategy == RoutingStrategy.Bubble ||
                args.RoutedEvent.RoutingStrategy == RoutingStrategy.Direct)
            {
                int endSourceChangeIndex = 0;
                
                // If the RoutingStrategy of the associated is 
                // Bubble the handlers for the last target 
                // added are the last ones invoked
                // Invoke class listeners
                for (int i=0; i<_routeItemList.Count; i++)
                {
                    // Query for new source only if we are 
                    // past the range of the previous source change
                    if (i >= endSourceChangeIndex)
                    {

View on GitHub (pinned to 81131a70a4)