{"record":{"id":"09c607a918d23e55","repo":"dotnet/wpf","slug":"invalidenumargumentexception-value-int-value-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"value\", (int)value, typeof(MouseAction))","messagePattern":"InvalidEnumArgumentException\\(\"value\", \\(int\\)value, typeof\\(MouseAction\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseGesture.cs","lineNumber":89,"sourceCode":"        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n\n#region Public Methods\n        /// <summary>\n        /// Action \n        /// </summary>\n        public MouseAction MouseAction\n        {\n            get \n            { \n                return _mouseAction; \n            }\n            set \n            {\n                if (!MouseGesture.IsDefinedMouseAction((MouseAction)value))\n                    throw new InvalidEnumArgumentException(\"value\", (int)value, typeof(MouseAction));\n                if (_mouseAction != value)\n                {\n                    _mouseAction = (MouseAction)value;\n                    OnPropertyChanged(nameof(MouseAction));\n                }\n            }\n        }\n\n        /// <summary>\n        /// Modifiers \n        /// </summary>\n        public ModifierKeys Modifiers\n        {\n            get \n            { \n                return _modifiers; \n            }\n            set ","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseGesture.cs#L71-L107","documentation":"The MouseGesture.MouseAction property setter validates the assigned value is a defined MouseAction enum member; otherwise it throws InvalidEnumArgumentException naming 'value'. This keeps an existing gesture from being mutated into an invalid state after construction.","triggerScenarios":"Assigning gesture.MouseAction = (MouseAction)42, typically from parsed data or computed int values.","commonSituations":"Programmatic shortcut editors, XAML data binding of an int to the MouseAction property, or refactorings that changed enum members leaving stale persisted values.","solutions":["Assign only valid MouseAction members; cast from int only after Enum.IsDefined check.","If the value comes from user input or config, map it through a parse-and-validate step before assignment.","Update stale persisted data to current MouseAction member names/values."],"exampleFix":"// before\ngesture.MouseAction = (MouseAction)storedValue;\n// after\nif (Enum.IsDefined(typeof(MouseAction), storedValue))\n    gesture.MouseAction = (MouseAction)storedValue;","handlingStrategy":"validation","validationCode":"if (Enum.IsDefined(typeof(MouseAction), newValue)) gesture.MouseAction = (MouseAction)newValue;","typeGuard":"static bool CanAssignMouseAction(int v) => Enum.IsDefined(typeof(MouseAction), v);","tryCatchPattern":"try { gesture.MouseAction = action; } catch (InvalidEnumArgumentException ex) { /* keep previous value, log */ }","preventionTips":["Validate parsed ints before property assignment","Prefer enum-typed bindings over int-typed ones","Add unit tests covering shortcut-editor input paths"],"tags":["wpf","enum","property-setter"],"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-22T01:17:13.364Z"}