dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("button", (int)button…
Error message
InvalidEnumArgumentException("button", (int)button, typeof(MouseButton)) What it means
MouseButton.Validate throws System.ComponentModel.InvalidEnumArgumentException when the button argument is not a defined MouseButton value (Left, Right, Middle, XButton1, XButton2). WPF validates public enum parameters to catch corrupted or miscomputed values early.
Solutions
- Validate the value against Enum.IsDefined(typeof(MouseButton), value) before passing it
- Map external button codes to MouseButton explicitly with a switch
- Fix the source that produces the out-of-range value
Example fix
// before
MouseButton b = (MouseButton)rawValue;
MouseButton.Validate(b); // throws
// after
if (!Enum.IsDefined(typeof(MouseButton), rawValue))
{
rawValue = (int)MouseButton.Left; // or map explicitly
}
MouseButton b = (MouseButton)rawValue; Defensive patterns
Strategy: validation
Validate before calling
static bool IsValidMouseButton(MouseButton b) =>
b == MouseButton.Left || b == MouseButton.Right || b == MouseButton.Middle ||
b == MouseButton.XButton1 || b == MouseButton.XButton2; Type guard
static bool TryParseMouseButton(int raw, out MouseButton button)
{ if (raw >= 0 && raw <= 4) { button = (MouseButton)raw; return true; } button = MouseButton.Left; return false; } Try / catch
try { MouseButton.Validate(button); }
catch (InvalidEnumArgumentException) { button = MouseButton.Left; } Prevention
- Map Win32/external button codes explicitly instead of casting ints
- Use Enum.IsDefined before any enum cast
- Never combine MouseButton values with bitwise operators
When it happens
Trigger: Passing an int cast to MouseButton that is outside 0-4, e.g. from P/Invoke data, deserialized state, or bit-flag arithmetic on button values.
Common situations: Interoperating with Win32 mouse messages where button codes differ, or computing a button from config/serialization that holds an out-of-range integer.
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("actions", (int)actions…
- InvalidEnumArgumentException("captureMode"…
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(userActivationMode)
- autoComplete
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c2cba1762260fbb9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/MouseButton.cs:75
/// <summary>
/// Ensures MouseButton is set to a valid value.
/// </summary>
/// <remarks>
/// There is a proscription against using Enum.IsDefined(). (it is slow)
/// So we manually validate using a switch statement.
/// </remarks>
internal static void Validate(MouseButton button)
{
switch(button)
{
case MouseButton.Left:
case MouseButton.Middle:
case MouseButton.Right:
case MouseButton.XButton1:
case MouseButton.XButton2:
break;
default:
throw new System.ComponentModel.InvalidEnumArgumentException("button", (int)button, typeof(MouseButton));
}
}
}
}
View on GitHub (pinned to 81131a70a4)