dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException("mouseAction"…

Error message

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

What it means

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.

Solutions

  1. Pass only declared MouseAction members (None, LeftClick, RightClick, MiddleClick, LeftDoubleClick, RightDoubleClick, MiddleDoubleClick).
  2. Before constructing, validate with Enum.IsDefined(typeof(MouseAction), value) and handle invalid input at the boundary.
  3. Fix the serialization/deserialization layer to emit valid enum names rather than raw ints.

Example fix

// before
var gesture = new MouseGesture((MouseAction)intFromConfig, ModifierKeys.Control);
// after
if (Enum.IsDefined(typeof(MouseAction), intFromConfig))
    var gesture = new MouseGesture((MouseAction)intFromConfig, ModifierKeys.Control);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(MouseAction), action)) throw new ArgumentOutOfRangeException(nameof(action));
var gesture = new MouseGesture(action, modifiers);

Type guard

static bool IsValidMouseAction(MouseAction a) => Enum.IsDefined(typeof(MouseAction), a);

Try / catch

try { var g = new MouseGesture(action, mods); } catch (InvalidEnumArgumentException ex) { /* fall back to MouseAction.None or reject input */ }

Prevention

When it happens

Trigger: new MouseGesture((MouseAction)999, ModifierKeys.None) — passing an int cast to MouseAction that is not a declared enum member.

Common situations: Loading gestures from config/serialization where raw integers were stored, or interop code passing Win32 mouse message values instead of MouseAction members.

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/99c3a4c9736f3e4f. Report an issue: GitHub.

Appendix: source

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

        }

        /// <summary>
        ///  constructor
        /// </summary>
        /// <param name="mouseAction">Mouse Action</param>
        public MouseGesture(MouseAction mouseAction): this(mouseAction, ModifierKeys.None)
        {
        }

        /// <summary>
        ///  Constructor
        /// </summary>
        /// <param name="mouseAction">Mouse Action</param>
        /// <param name="modifiers">Modifiers</param>
        public MouseGesture( MouseAction mouseAction,ModifierKeys modifiers)   // acclerator action
        {
            if (!MouseGesture.IsDefinedMouseAction(mouseAction))
                throw new InvalidEnumArgumentException("mouseAction", (int)mouseAction, typeof(MouseAction));

            if (!ModifierKeysConverter.IsDefinedModifierKeys(modifiers))
                throw new InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys));

            _modifiers = modifiers;
            _mouseAction = mouseAction;

            //AttachClassListeners();
        }
#endregion Constructors
        
        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

#region Public Methods

View on GitHub (pinned to 81131a70a4)