dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException("value", (int)value…

Error message

InvalidEnumArgumentException("value", (int)value, typeof(MouseAction))

What it means

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.

Solutions

  1. Assign only valid MouseAction members; cast from int only after Enum.IsDefined check.
  2. If the value comes from user input or config, map it through a parse-and-validate step before assignment.
  3. Update stale persisted data to current MouseAction member names/values.

Example fix

// before
gesture.MouseAction = (MouseAction)storedValue;
// after
if (Enum.IsDefined(typeof(MouseAction), storedValue))
    gesture.MouseAction = (MouseAction)storedValue;
Defensive patterns

Strategy: validation

Validate before calling

if (Enum.IsDefined(typeof(MouseAction), newValue)) gesture.MouseAction = (MouseAction)newValue;

Type guard

static bool CanAssignMouseAction(int v) => Enum.IsDefined(typeof(MouseAction), v);

Try / catch

try { gesture.MouseAction = action; } catch (InvalidEnumArgumentException ex) { /* keep previous value, log */ }

Prevention

When it happens

Trigger: Assigning gesture.MouseAction = (MouseAction)42, typically from parsed data or computed int values.

Common situations: Programmatic shortcut editors, XAML data binding of an int to the MouseAction property, or refactorings that changed enum members leaving stale persisted values.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/09c607a918d23e55. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseGesture.cs:89

        //
        //  Public Methods
        //
        //------------------------------------------------------

#region Public Methods
        /// <summary>
        /// Action 
        /// </summary>
        public MouseAction MouseAction
        {
            get 
            { 
                return _mouseAction; 
            }
            set 
            {
                if (!MouseGesture.IsDefinedMouseAction((MouseAction)value))
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(MouseAction));
                if (_mouseAction != value)
                {
                    _mouseAction = (MouseAction)value;
                    OnPropertyChanged(nameof(MouseAction));
                }
            }
        }

        /// <summary>
        /// Modifiers 
        /// </summary>
        public ModifierKeys Modifiers
        {
            get 
            { 
                return _modifiers; 
            }
            set 

View on GitHub (pinned to 81131a70a4)