dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException("value", (int)value…

Error message

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

What it means

The MouseGesture.Modifiers property setter validates the assigned value via ModifierKeysConverter.IsDefinedModifierKeys; an undefined ModifierKeys value throws InvalidEnumArgumentException naming 'value'.

Solutions

  1. Assign only valid ModifierKeys members or combinations of Alt/Control/Shift/Windows.
  2. Validate with ModifierKeysConverter.IsDefinedModifierKeys((ModifierKeys)value) before assignment.
  3. Mask the incoming value to the defined modifier bits and reject unknown bits explicitly.

Example fix

// before
gesture.Modifiers = (ModifierKeys)incomingFlags;
// after
var mods = (ModifierKeys)incomingFlags;
if (ModifierKeysConverter.IsDefinedModifierKeys(mods))
    gesture.Modifiers = mods;
Defensive patterns

Strategy: validation

Validate before calling

var mods = (ModifierKeys)value;
if (ModifierKeysConverter.IsDefinedModifierKeys(mods)) gesture.Modifiers = mods;

Type guard

static bool CanAssignModifiers(ModifierKeys m) => ModifierKeysConverter.IsDefinedModifierKeys(m);

Try / catch

try { gesture.Modifiers = mods; } catch (InvalidEnumArgumentException ex) { /* reset to ModifierKeys.None */ }

Prevention

When it happens

Trigger: Assigning gesture.Modifiers = (ModifierKeys)128 from raw int data or flag arithmetic that produced undefined bit combinations.

Common situations: Binding layers or shortcut-customization UIs writing computed integer flags, or cross-platform code mapping foreign modifier bits into ModifierKeys.

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

Appendix: source

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

                    _mouseAction = (MouseAction)value;
                    OnPropertyChanged(nameof(MouseAction));
                }
            }
        }

        /// <summary>
        /// Modifiers 
        /// </summary>
        public ModifierKeys Modifiers
        {
            get 
            { 
                return _modifiers; 
            }
            set 
            {
                if (!ModifierKeysConverter.IsDefinedModifierKeys((ModifierKeys)value))
                    throw new InvalidEnumArgumentException("value", (int)value, typeof(ModifierKeys));

                if (_modifiers != value)
                {
                    _modifiers = (ModifierKeys)value;
                    OnPropertyChanged(nameof(Modifiers));
                }
            }
        }

        /// <summary>
        ///     Compares InputEventArgs with current Input
        /// </summary>
        /// <param name="targetElement">the element to receive the command</param>
        /// <param name="inputEventArgs">inputEventArgs to compare to</param>
        /// <returns>True - if matches, false otherwise.
        /// </returns>
        public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
        {

View on GitHub (pinned to 81131a70a4)