dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException(nameof(value)…
Error message
InvalidEnumArgumentException(nameof(value), (int)mouseAction, typeof(MouseAction))
What it means
MouseActionConverter.ConvertTo accepts only the seven defined MouseAction values; anything else throws InvalidEnumArgumentException for 'value'. This converts the enum back to its string form for serialization/XAML output, so undefined enum values cannot be represented.
Solutions
- Only pass defined MouseAction enum members (LeftClick..MiddleDoubleClick)
- Validate the value against the known members before calling ConvertTo
- Sanitize config data with the enum converter when loading
Example fix
// before
string s = (string)converter.ConvertTo(null, culture, (MouseAction)rawInt, typeof(string));
// after
var action = (MouseAction)rawInt;
bool defined = action is MouseAction.LeftClick or MouseAction.RightClick or MouseAction.MiddleClick
or MouseAction.WheelClick or MouseAction.LeftDoubleClick or MouseAction.RightDoubleClick or MouseAction.MiddleDoubleClick;
if (defined)
{
string s = (string)converter.ConvertTo(null, culture, action, typeof(string));
} Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(MouseAction), mouseAction)) throw new ArgumentOutOfRangeException(nameof(mouseAction));
Type guard
static bool IsDefinedMouseAction(MouseAction a) => a is MouseAction.LeftClick or MouseAction.RightClick or MouseAction.MiddleClick or MouseAction.WheelClick or MouseAction.LeftDoubleClick or MouseAction.RightDoubleClick or MouseAction.MiddleDoubleClick;
Try / catch
try { s = (string)converter.ConvertTo(null, culture, mouseAction, typeof(string)); } catch (InvalidEnumArgumentException ex) { /* undefined MouseAction value */ } Prevention
- Never cast raw ints to MouseAction without Enum.IsDefined validation
- Sanitize persisted gesture data when loading from config
When it happens
Trigger: Calling ConvertTo with a MouseAction cast from an undefined int (e.g. (MouseAction)42 or negative values).
Common situations: Serializing gesture settings that were loaded from untrusted/config data with out-of-range integers.
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
- SR.Unsupported_MouseAction (mouseActionToken)
- InvalidEnumArgumentException("action", (int)action…
- InvalidEnumArgumentException("actions", (int)actions…
- InvalidEnumArgumentException("actions", (int)actions…
- InvalidEnumArgumentException("key", (int)key, typeof(Key))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/4c15134a95f6a86f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs:100
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
ArgumentNullException.ThrowIfNull(destinationType);
if (value is null || destinationType != typeof(string))
throw GetConvertToException(value, destinationType);
MouseAction mouseAction = (MouseAction)value;
return mouseAction switch
{
MouseAction.None => string.Empty,
MouseAction.LeftClick => "LeftClick",
MouseAction.RightClick => "RightClick",
MouseAction.MiddleClick => "MiddleClick",
MouseAction.WheelClick => "WheelClick",
MouseAction.LeftDoubleClick => "LeftDoubleClick",
MouseAction.RightDoubleClick => "RightDoubleClick",
MouseAction.MiddleDoubleClick => "MiddleDoubleClick",
_ => throw new InvalidEnumArgumentException(nameof(value), (int)mouseAction, typeof(MouseAction))
};
}
/// <summary>
/// Helper function similar to <see cref="Enum.IsDefined{MouseAction}(MouseAction)"/>, just lighter and faster.
/// </summary>
/// <param name="mouseAction">The value to test against.</param>
/// <returns><see langword="true"/> if <paramref name="mouseAction"/> falls in enumeration range, <see langword="false"/> otherwise.</returns>
internal static bool IsDefinedMouseAction(MouseAction mouseAction)
{
return mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick;
}
}
}
View on GitHub (pinned to 81131a70a4)