{"record":{"id":"d49683791ac1c487","repo":"dotnet/wpf","slug":"invalidenumargumentexception-routingstrategy","errorCode":null,"errorMessage":"InvalidEnumArgumentException: routingStrategy","messagePattern":"InvalidEnumArgumentException: routingStrategy","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs","lineNumber":61,"sourceCode":"        ///     <see cref=\"RoutedEvent.OwnerType\"/>\n        /// </param>\n        /// <returns>\n        ///     The new registered <see cref=\"RoutedEvent\"/>\n        /// </returns>\n        /// <ExternalAPI/>\n        public static RoutedEvent RegisterRoutedEvent(\n            string name,\n            RoutingStrategy routingStrategy,\n            Type handlerType,\n            Type ownerType)\n        {\n            ArgumentNullException.ThrowIfNull(name);\n\n            if (routingStrategy != RoutingStrategy.Tunnel && \n                routingStrategy != RoutingStrategy.Bubble &&\n                routingStrategy != RoutingStrategy.Direct) \n            {\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"routingStrategy\", (int)routingStrategy, typeof(RoutingStrategy));\n            }\n\n            ArgumentNullException.ThrowIfNull(handlerType);\n\n            ArgumentNullException.ThrowIfNull(ownerType);\n\n            if (GlobalEventManager.GetRoutedEventFromName(name, ownerType, false) != null)\n            {\n                throw new ArgumentException(SR.Format(SR.DuplicateEventName, name, ownerType)); \n            }\n\n            return GlobalEventManager.RegisterRoutedEvent(name, routingStrategy, handlerType, ownerType);\n        }\n\n        /// <summary>\n        ///     See overloaded method for details\n        /// </summary>\n        /// <remarks>","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs#L43-L79","documentation":"EventManager.RegisterRoutedEvent requires routingStrategy to be one of Tunnel, Bubble, or Direct. Any other value of the RoutingStrategy enum triggers InvalidEnumArgumentException naming the parameter 'routingStrategy'. This guards against invalid enum casts at the public registration API.","triggerScenarios":"Calling RegisterRoutedEvent with a RoutingStrategy value obtained via an unchecked cast, e.g. (RoutingStrategy)99, or a default/uninitialized enum value.","commonSituations":"Reading the strategy from configuration or data where it was stored as int; reflection-driven event registration; deserialization assigning numeric enum values.","solutions":["Pass a valid RoutingStrategy: Tunnel, Bubble, or Direct","Validate/whitelist the int source value with Enum.IsDefined before casting","Fix the code that produced the out-of-range enum value"],"exampleFix":"// before\nvar strategy = (RoutingStrategy)userValue; // e.g. 7\nEventManager.RegisterRoutedEvent(\"MyEvent\", strategy, typeof(EventHandler), typeof(MyControl));\n// after\nif (Enum.IsDefined(typeof(RoutingStrategy), userValue))\n    EventManager.RegisterRoutedEvent(\"MyEvent\", (RoutingStrategy)userValue, typeof(EventHandler), typeof(MyControl));\nelse\n    EventManager.RegisterRoutedEvent(\"MyEvent\", RoutingStrategy.Bubble, typeof(EventHandler), typeof(MyControl));","handlingStrategy":"validation","validationCode":"if (Enum.IsDefined(typeof(RoutingStrategy), routingStrategy)) { /* safe */ }","typeGuard":"bool IsValidRoutingStrategy(RoutingStrategy s) => s is RoutingStrategy.Tunnel or RoutingStrategy.Bubble or RoutingStrategy.Direct;","tryCatchPattern":"try { EventManager.RegisterRoutedEvent(name, strategy, handlerType, ownerType); } catch (InvalidEnumArgumentException) { strategy = RoutingStrategy.Bubble; }","preventionTips":["Never cast ints to RoutingStrategy without Enum.IsDefined","Use pattern matching against the three valid members","Store routing strategies as enum values, not raw ints in config"],"tags":["wpf","routed-events","enum","argument"],"backgroundTag":"invalid-enum-value","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"}