dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("focusNavigationDirection"…
Error message
InvalidEnumArgumentException("focusNavigationDirection", (int)focusNavigationDirection, typeof(FocusNavigationDirection)) What it means
The TraversalRequest constructor validates that focusNavigationDirection is a defined member of the FocusNavigationDirection enum, throwing InvalidEnumArgumentException otherwise. This prevents invalid navigation directions from entering the focus traversal system. It is a fail-fast guard at the public constructor boundary.
Solutions
- Validate with Enum.IsDefined(typeof(FocusNavigationDirection), direction) before constructing TraversalRequest.
- Use one of the named enum constants (Next, Previous, First, Last, Left, Right, Up, Down) instead of computed integers.
- Clamp or normalize the computed direction into the valid range before constructing.
- Catch InvalidEnumArgumentException and fall back to FocusNavigationDirection.Next.
Example fix
// before
var request = new TraversalRequest((FocusNavigationDirection)offset); // offset arbitrary
// after
if (Enum.IsDefined(typeof(FocusNavigationDirection), offset))
var request = new TraversalRequest((FocusNavigationDirection)offset);
else
var request = new TraversalRequest(FocusNavigationDirection.Next); Defensive patterns
Strategy: validation
Validate before calling
bool ok = Enum.IsDefined(typeof(FocusNavigationDirection), direction);
Type guard
static bool IsValidFocusNavDirection(FocusNavigationDirection d) => Enum.IsDefined(typeof(FocusNavigationDirection), d);
Try / catch
try { var req = new TraversalRequest(direction); }
catch (InvalidEnumArgumentException) { var req = new TraversalRequest(FocusNavigationDirection.Next); } Prevention
- Use named enum constants, not computed ints.
- Validate with Enum.IsDefined after any arithmetic on enum values.
- Be careful casting persisted/native ints back to the enum.
When it happens
Trigger: new TraversalRequest((FocusNavigationDirection)someInt) where the int is negative or beyond the last defined enum member (FocusNavigationDirection has values 0-6, so anything outside that range).
Common situations: Computing a navigation direction arithmetically from a key press or index; deserializing persisted UI state with a stale int; interop code passing unvalidated native values.
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
- Animation_UnrecognizedHandoffBehavior
- args
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(authenticationType)
- ArgumentOutOfRangeException(userActivationMode)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/95365fe094a02c17.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/TraversalRequest.cs:27
if (focusNavigationDirection != FocusNavigationDirection.Next &&
focusNavigationDirection != FocusNavigationDirection.Previous &&
focusNavigationDirection != FocusNavigationDirection.First &&
focusNavigationDirection != FocusNavigationDirection.Last &&
focusNavigationDirection != FocusNavigationDirection.Left &&
focusNavigationDirection != FocusNavigationDirection.Right &&
focusNavigationDirection != FocusNavigationDirection.Up &&
focusNavigationDirection != FocusNavigationDirection.Down)
{
throw new System.ComponentModel.InvalidEnumArgumentException("focusNavigationDirection", (int)focusNavigationDirection, typeof(FocusNavigationDirection));
}
_focusNavigationDirection = focusNavigationDirection;View on GitHub (pinned to 81131a70a4)