{"record":{"id":"ebaa8f3517a17801","repo":"dotnet/wpf","slug":"sr-sourcenotset","errorCode":null,"errorMessage":"SR.SourceNotSet","messagePattern":"SR\\.SourceNotSet","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventRoute.cs","lineNumber":128,"sourceCode":"        internal void InvokeHandlers(object source, RoutedEventArgs args)\n        {\n            InvokeHandlersImpl(source, args, false);\n        }\n\n        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++)","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventRoute.cs#L110-L146","documentation":"EventRoute.InvokeHandlersImpl requires the RoutedEventArgs to have its Source set before the route can be raised. If args.Source is null it throws ArgumentException with SR.SourceNotSet. Source is used to start/continue route traversal, so an unset source makes routing impossible.","triggerScenarios":"Invoking the route (via InvokeHandlers/ReInvokeHandlers or RaiseEvent plumbing) with a RoutedEventArgs instance whose Source was never set — e.g. constructing a new RoutedEventArgs and calling route.InvokeHandlers manually without assigning Source.","commonSituations":"Custom routing code building an EventRoute by hand; re-raising a cloned event args where Source was not copied; unit tests invoking routes directly.","solutions":["Set args.Source (or call args.SetSource / raise via UIElement.RaiseEvent which sets Source) before invoking the route","Ensure custom event args constructors assign a non-null Source","Route the event through element.RaiseEvent rather than manual EventRoute invocation"],"exampleFix":"// before\nvar args = new RoutedEventArgs(MyEvent);\nroute.InvokeHandlers(this, args);\n// after\nvar args = new RoutedEventArgs(MyEvent);\nargs.Source = sourceElement;\nroute.InvokeHandlers(this, args);","handlingStrategy":"validation","validationCode":"if (args != null && args.Source != null) { route.InvokeHandlers(this, args); }","typeGuard":"bool HasSource(RoutedEventArgs a) => a != null && a.Source != null;","tryCatchPattern":"try { route.InvokeHandlers(this, args); } catch (ArgumentException) { /* args.Source was null */ }","preventionTips":["Prefer element.RaiseEvent, which sets Source automatically","Initialize Source in custom RoutedEventArgs constructors","Never build/invoke EventRoute manually without setting Source"],"tags":["wpf","routed-events","eventargs","argument"],"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-22T01:17:13.364Z"}