{"record":{"id":"99c3a4c9736f3e4f","repo":"dotnet/wpf","slug":"invalidenumargumentexception-mouseaction-int-mouseaction","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"mouseAction\", (int)mouseAction, typeof(MouseAction))","messagePattern":"InvalidEnumArgumentException\\(\"mouseAction\", \\(int\\)mouseAction, typeof\\(MouseAction\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseGesture.cs","lineNumber":58,"sourceCode":"        }\n\n        /// <summary>\n        ///  constructor\n        /// </summary>\n        /// <param name=\"mouseAction\">Mouse Action</param>\n        public MouseGesture(MouseAction mouseAction): this(mouseAction, ModifierKeys.None)\n        {\n        }\n\n        /// <summary>\n        ///  Constructor\n        /// </summary>\n        /// <param name=\"mouseAction\">Mouse Action</param>\n        /// <param name=\"modifiers\">Modifiers</param>\n        public MouseGesture( MouseAction mouseAction,ModifierKeys modifiers)   // acclerator action\n        {\n            if (!MouseGesture.IsDefinedMouseAction(mouseAction))\n                throw new InvalidEnumArgumentException(\"mouseAction\", (int)mouseAction, typeof(MouseAction));\n\n            if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers))\n                throw new InvalidEnumArgumentException(\"modifiers\", (int)modifiers, typeof(ModifierKeys));\n\n            _modifiers = modifiers;\n            _mouseAction = mouseAction;\n\n            //AttachClassListeners();\n        }\n#endregion Constructors\n        \n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n\n#region Public Methods","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseGesture.cs#L40-L76","documentation":"The MouseGesture(MouseAction, ModifierKeys) constructor validates that mouseAction is a defined MouseAction enum value; an undefined numeric cast throws InvalidEnumArgumentException naming the 'mouseAction' parameter. WPF guards enum parameters against values never declared in the enum.","triggerScenarios":"new MouseGesture((MouseAction)999, ModifierKeys.None) — passing an int cast to MouseAction that is not a declared enum member.","commonSituations":"Loading gestures from config/serialization where raw integers were stored, or interop code passing Win32 mouse message values instead of MouseAction members.","solutions":["Pass only declared MouseAction members (None, LeftClick, RightClick, MiddleClick, LeftDoubleClick, RightDoubleClick, MiddleDoubleClick).","Before constructing, validate with Enum.IsDefined(typeof(MouseAction), value) and handle invalid input at the boundary.","Fix the serialization/deserialization layer to emit valid enum names rather than raw ints."],"exampleFix":"// before\nvar gesture = new MouseGesture((MouseAction)intFromConfig, ModifierKeys.Control);\n// after\nif (Enum.IsDefined(typeof(MouseAction), intFromConfig))\n    var gesture = new MouseGesture((MouseAction)intFromConfig, ModifierKeys.Control);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(MouseAction), action)) throw new ArgumentOutOfRangeException(nameof(action));\nvar gesture = new MouseGesture(action, modifiers);","typeGuard":"static bool IsValidMouseAction(MouseAction a) => Enum.IsDefined(typeof(MouseAction), a);","tryCatchPattern":"try { var g = new MouseGesture(action, mods); } catch (InvalidEnumArgumentException ex) { /* fall back to MouseAction.None or reject input */ }","preventionTips":["Never cast raw ints to MouseAction without Enum.IsDefined","Serialize enums by name, not numeric value","Validate external/config data at the deserialization boundary"],"tags":["wpf","enum","invalid-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"}