dotnet/wpf · error · ArgumentException
SR.Mismatched_RoutedEvent
Error message
SR.Mismatched_RoutedEvent
What it means
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.
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
Example fix
// before var args = new RoutedEventArgs(OtherEvent); element.RaiseEvent(MyEvent, args); // mismatch // after var args = new RoutedEventArgs(MyEvent); element.RaiseEvent(MyEvent, args);
Defensive patterns
Strategy: validation
Validate before calling
if (args.RoutedEvent != route.RoutedEvent) throw new InvalidOperationException("RoutedEvent mismatch before RaiseEvent");
element.RaiseEvent(args); Type guard
static bool MatchesRoute(RoutedEvent routeEvent, RoutedEventArgs args) => args?.RoutedEvent == routeEvent;
Try / catch
try { element.RaiseEvent(args); }
catch (ArgumentException ex) when (ex.Message.Contains("RoutedEvent")) { log.Warn("Event/args mismatch", ex); } Prevention
- 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
When it happens
Trigger: 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.
Common situations: Pooled/shared RoutedEventArgs instances after refactoring event definitions; copy-pasted RaiseEvent calls where the event field was renamed; framework code routing through EventRoute manually.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- SR.Invalid_IInputElement
- SR.HandlerTypeIllegal
- SR.IncorrectLocatorPartType
- SR.MustBeTriggerAction
- SR.SourceNotSet
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5bc8a6ac4dae2ec4.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs:1649
{
// VerifyAccess();
ContextLayoutManager.From(Dispatcher).UpdateLayout();
}
internal static void BuildRouteHelper(DependencyObject e, EventRoute route, RoutedEventArgs args)
{
ArgumentNullException.ThrowIfNull(route);
ArgumentNullException.ThrowIfNull(args);
if (args.Source == null)
{
throw new ArgumentException(SR.SourceNotSet);
}
if (args.RoutedEvent != route.RoutedEvent)
{
throw new ArgumentException(SR.Mismatched_RoutedEvent);
}
// Route via visual tree
if (args.RoutedEvent.RoutingStrategy == RoutingStrategy.Direct)
{
UIElement uiElement = e as UIElement;
ContentElement contentElement = null;
UIElement3D uiElement3D = null;
if (uiElement == null)
{
contentElement = e as ContentElement;
if (contentElement == null)
{
uiElement3D = e as UIElement3D;
}
}View on GitHub (pinned to 81131a70a4)