{"record":{"id":"c8594e5eed38e350","repo":"dotnet/wpf","slug":"sr-routedeventargsmusthaveroutedevent","errorCode":null,"errorMessage":"SR.RoutedEventArgsMustHaveRoutedEvent","messagePattern":"SR\\.RoutedEventArgsMustHaveRoutedEvent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/RoutedEventArgs.cs","lineNumber":130,"sourceCode":"        ///     Returns a boolean flag indicating if or not this\n        ///     RoutedEvent has been handled this far in the route\n        /// </summary>\n        /// <remarks>\n        ///     Initially starts with a false value before routing\n        ///     has begun\n        /// </remarks>\n        public bool Handled\n        {\n            get\n            {\n                return _flags[ HandledIndex ] ;\n            }\n\n            set\n            {\n                if (_routedEvent == null)\n                {\n                    throw new InvalidOperationException(SR.RoutedEventArgsMustHaveRoutedEvent);\n                }\n\n\n                if( TraceRoutedEvent.IsEnabled )\n                {\n                    TraceRoutedEvent.TraceActivityItem(\n                                            TraceRoutedEvent.HandleEvent,\n                                            value,\n                                            RoutedEvent.OwnerType.Name,\n                                            RoutedEvent.Name,\n                                            this );\n                }\n\n\n                // Note: We need to allow the caller to change the handled value\n                // from true to false.\n                //\n                // We are concerned about scenarios where a child element","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/RoutedEventArgs.cs#L112-L148","documentation":"RoutedEventArgs.RoutedEvent property setter throws this InvalidOperationException when the event args object has no associated RoutedEvent set. WPF routed event arguments must be tied to a RoutedEvent before properties like Source can be manipulated, because routing behavior depends on it. The library enforces this to keep the routed-event pipeline consistent.","triggerScenarios":"Setting RoutedEventArgs.Source (or Handled/other guarded properties) on a RoutedEventArgs instance whose RoutedEvent field is still null — i.e. args constructed directly and never raised through RoutedEvent.RaiseEvent.","commonSituations":"Developers newing up RoutedEventArgs or a subclass and assigning Source manually before raising it; reusing event args outside a real RaiseEvent call; testing event args in isolation.","solutions":["Set the RoutedEvent property (or pass it to the constructor) before touching Source.","Raise the event via UIElement.RaiseEvent so the framework initializes the args.","If building custom args, use the constructor overload that accepts a RoutedEvent."],"exampleFix":"// before\nvar args = new RoutedEventArgs();\nelement.RaiseEvent(args);\n// after\nvar args = new RoutedEventArgs(MyControl.MyRoutedEvent);\nelement.RaiseEvent(args);","handlingStrategy":"validation","validationCode":"if (args.RoutedEvent == null) throw new InvalidOperationException(\"Set RoutedEvent before using these args\");","typeGuard":null,"tryCatchPattern":"try { args.Source = target; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"RoutedEvent\")) { /* attach RoutedEvent and retry */ }","preventionTips":["Always construct RoutedEventArgs with the RoutedEvent-taking constructor","Never reuse event args objects across raises without resetting RoutedEvent","Prefer element.RaiseEvent over manual args manipulation"],"tags":["wpf","routed-events","invalid-state"],"backgroundTag":"invalid-state-transition","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"}