{"record":{"id":"efd8211a0ce787f6","repo":"dotnet/wpf","slug":"sr-sourcenotset-uielement","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/UIElement.cs","lineNumber":1644,"sourceCode":"        /// This method does nothing if layout is clean but it does work if layout is not clean so avoid calling\n        /// it after each change in the element tree. It makes sense to either never call it (system will do this\n        /// in a deferred manner) or only call it if you absolutely need updated sizes and positions after you do all changes.\n        /// </remarks>\n        public void UpdateLayout()\n        {\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","sourceCodeStart":1626,"sourceCodeEnd":1662,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs#L1626-L1662","documentation":"UIElement.RaiseEvent (and related routing entry points) requires the RoutedEventArgs to carry a Source. When args.Source is null the event cannot be routed sensibly, so an ArgumentException with SR.SourceNotSet is thrown. The library refuses to raise an event whose origin is unknown.","triggerScenarios":"Calling RaiseEvent / RaiseImpl with a hand-constructed RoutedEventArgs (e.g. new RoutedEventArgs(SomeEvent)) without setting Source before raising.","commonSituations":"Custom control authors raising routed events from non-UI helper classes; replaying or synthesizing events from unit tests or message-processing code; deserializing event args.","solutions":["Set args.Source (e.g. args.Source = this;) before calling RaiseEvent","Use UIElement.RaiseEvent with args whose RoutedEvent and Source are populated","In test/synthesis code, construct RoutedEventArgs via a constructor overload that sets source"],"exampleFix":"// before\nvar args = new RoutedEventArgs(MyEvent);\nelement.RaiseEvent(args); // throws\n// after\nvar args = new RoutedEventArgs(MyEvent) { Source = element };\nelement.RaiseEvent(args);","handlingStrategy":"validation","validationCode":"if (args == null) throw new ArgumentNullException(nameof(args));\nif (args.Source == null) args.Source = this; // or refuse to raise\nelement.RaiseEvent(args);","typeGuard":"static bool CanRaise(RoutedEventArgs args) => args?.Source != null && args.RoutedEvent != null;","tryCatchPattern":"try { element.RaiseEvent(args); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Source\")) { args.Source = element; element.RaiseEvent(args); }","preventionTips":["Always set Source on hand-built RoutedEventArgs","Prefer constructor overloads or helper methods that populate Source","Never re-raise args after clearing/losing their Source"],"tags":["wpf","routed-events","argument-validation"],"backgroundTag":"null-argument","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"}