dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException(argumentName, direction…
Error message
InvalidEnumArgumentException(argumentName, direction, LogicalDirection)
What it means
ValidationHelper.VerifyDirection rejects any LogicalDirection value other than Forward or Backward, throwing InvalidEnumArgumentException with the caller-supplied argument name. LogicalDirection is a two-value enum; anything else (e.g. a cast invalid int) is an invalid enum argument.
Solutions
- Pass only LogicalDirection.Forward or LogicalDirection.Backward.
- Validate/parse the source int with Enum.IsDefined before casting.
- Clamp or normalize persisted values at load time.
Example fix
// before var dir = (LogicalDirection)userValue; // userValue = 5 // after var dir = Enum.IsDefined(typeof(LogicalDirection), userValue) ? (LogicalDirection)userValue : LogicalDirection.Forward;
Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(LogicalDirection), value)) throw new InvalidEnumArgumentException(nameof(value), value, typeof(LogicalDirection));
Type guard
bool IsValidLogicalDirection(object v) => v is LogicalDirection d && (d == LogicalDirection.Forward || d == LogicalDirection.Backward);
Try / catch
try { ... } catch (InvalidEnumArgumentException ex) { /* repair or default the direction */ } Prevention
- Never cast raw ints to LogicalDirection without Enum.IsDefined
- Validate persisted enum values at deserialization time
When it happens
Trigger: Passing a LogicalDirection value obtained by casting an out-of-range int (e.g. (LogicalDirection)2) to any API that routes through VerifyDirection.
Common situations: Deserializing enum values from config or persisted data where the raw int is unvalidated; arithmetic on enum 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
- InvalidEnumArgumentException("mouseAction"…
- InvalidEnumArgumentException(param, edge, ElementEdge)
- InvalidEnumArgumentException("value", (int)value…
- ThemeMode value is invalid. Use None, System, Light or Dark
- Animation_UnrecognizedHandoffBehavior
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/2f34b864b9250c8c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs:73
ArgumentNullException.ThrowIfNull(startPosition);
ArgumentNullException.ThrowIfNull(endPosition);
if (startPosition.TextContainer != endPosition.TextContainer)
{
throw new ArgumentException(SR.Format(SR.InDifferentTextContainers, "startPosition", "endPosition"));
}
if (startPosition.CompareTo(endPosition) > 0)
{
throw new ArgumentException(SR.Format(SR.BadTextPositionOrder, "startPosition", "endPosition"));
}
}
// Throws an ArgumentException if direction is not a valid enum.
internal static void VerifyDirection(LogicalDirection direction, string argumentName)
{
if (direction != LogicalDirection.Forward &&
direction != LogicalDirection.Backward)
{
throw new InvalidEnumArgumentException(argumentName, (int)direction, typeof(LogicalDirection));
}
}
// Throws an ArgumentException if edge is not a valid enum.
internal static void VerifyElementEdge(ElementEdge edge, string param)
{
if (edge != ElementEdge.BeforeStart &&
edge != ElementEdge.AfterStart &&
edge != ElementEdge.BeforeEnd &&
edge != ElementEdge.AfterEnd)
{
throw new InvalidEnumArgumentException(param, (int)edge, typeof(ElementEdge));
}
}
// ...............................................................
//
// TextSchema ValidationView on GitHub (pinned to 81131a70a4)