dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("modifiers", (int)modifiers…
Error message
InvalidEnumArgumentException("modifiers", (int)modifiers, typeof(ModifierKeys)) What it means
The same MouseGesture(MouseAction, ModifierKeys) constructor also validates that modifiers is a defined ModifierKeys value; an undefined value throws InvalidEnumArgumentException naming the 'modifiers' parameter.
Solutions
- Use only ModifierKeys enum members or valid combinations of them via the | operator.
- Validate with ModifierKeysConverter.IsDefinedModifierKeys(modifiers) (or mask-check against the defined flags) before constructing.
- Normalize incoming modifier data at deserialization time to the nearest valid ModifierKeys.
Example fix
// before var gesture = new MouseGesture(MouseAction.RightClick, (ModifierKeys)rawBits); // after var mods = (ModifierKeys)rawBits & (ModifierKeys.Alt | ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Windows); var gesture = new MouseGesture(MouseAction.RightClick, mods);
Defensive patterns
Strategy: validation
Validate before calling
var allowed = ModifierKeys.Alt | ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Windows; if (((ModifierKeys)raw & ~allowed) != 0 || (ModifierKeys)raw == 0 && raw != 0) throw new ArgumentOutOfRangeException(nameof(raw)); var gesture = new MouseGesture(action, (ModifierKeys)raw);
Type guard
static bool IsValidModifiers(ModifierKeys m) => (m & ~(ModifierKeys.Alt|ModifierKeys.Control|ModifierKeys.Shift|ModifierKeys.Windows)) == 0;
Try / catch
try { var g = new MouseGesture(action, mods); } catch (InvalidEnumArgumentException ex) { /* normalize modifiers and retry */ } Prevention
- Build modifier values only from named ModifierKeys members with |
- Mask unknown bits out of persisted modifier ints
- Round-trip test serialized gestures
When it happens
Trigger: new MouseGesture(MouseAction.LeftClick, (ModifierKeys)64) — an int cast to ModifierKeys outside defined members (None, Alt, Control, Shift, Windows and defined combinations).
Common situations: Persisting modifier state as an int and combining flags ad hoc, or macOS/Linux key mappings translated to undefined ModifierKeys bits.
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
- InvalidEnumArgumentException("value", (int)value…
- Animation_UnrecognizedHandoffBehavior
- args
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(authenticationType)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/85fe4bcd053972b3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseGesture.cs:61
/// 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
/// <summary>
/// Action
/// </summary>View on GitHub (pinned to 81131a70a4)