dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException(direction, (int)direction…
Error message
InvalidEnumArgumentException(direction, (int)direction, typeof(ListSortDirection))
What it means
The SortDescription constructor validates that direction is exactly ListSortDirection.Ascending or Descending. Any other cast integer/enum value throws InvalidEnumArgumentException naming the 'direction' parameter, guarding against undefined sort directions.
Solutions
- Pass only ListSortDirection.Ascending or ListSortDirection.Descending
- Enum.IsDefined(typeof(ListSortDirection), value) before casting and constructing
- Map legacy/unknown stored values to a valid direction with a default fallback
Example fix
// before var sd = new SortDescription(prop, (ListSortDirection)rawValue); // after var dir = Enum.IsDefined(typeof(ListSortDirection), rawValue) ? (ListSortDirection)rawValue : ListSortDirection.Ascending; var sd = new SortDescription(prop, dir);
Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(ListSortDirection), direction)) throw new InvalidEnumArgumentException(nameof(direction), (int)direction, typeof(ListSortDirection));
Type guard
static bool IsValidSortDirection(ListSortDirection d) => d == ListSortDirection.Ascending || d == ListSortDirection.Descending;
Try / catch
try { var sd = new SortDescription(name, dir); } catch (InvalidEnumArgumentException) { dir = ListSortDirection.Ascending; } Prevention
- Never cast raw ints to ListSortDirection without Enum.IsDefined
- Persist sort directions as enum names, not numbers
- Clamp deserialized values to Ascending by default
When it happens
Trigger: new SortDescription("Name", (ListSortDirection)2) or passing an int-cast/unknown enum value from config, data binding, or deserialization.
Common situations: Deserializing sort settings from XML/JSON where the stored integer no longer matches the enum; bitwise-combining enum values incorrectly; cross-version enum changes.
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
- ArgumentOutOfRangeException(authentication)
- ArgumentOutOfRangeException(userActivationMode)
- autoComplete
- captureMode
- Enum_Invalid
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/2d1c16e793e89705.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescription.cs:28
{
//------------------------------------------------------
//
// Public Constructors
//
//------------------------------------------------------
#region Public Constructors
/// <summary>
/// Create a sort description.
/// </summary>
/// <param name="propertyName">Property to sort by</param>
/// <param name="direction">Specifies the direction of sort operation</param>
/// <exception cref="InvalidEnumArgumentException"> direction is not a valid value for ListSortDirection </exception>
public SortDescription(string propertyName, ListSortDirection direction)
{
if (direction != ListSortDirection.Ascending && direction != ListSortDirection.Descending)
throw new InvalidEnumArgumentException("direction", (int)direction, typeof(ListSortDirection));
_propertyName = propertyName;
_direction = direction;
_sealed = false;
}
#endregion Public Constructors
//------------------------------------------------------
//
// Public Properties
//
//------------------------------------------------------
#region Public Properties
/// <summary>View on GitHub (pinned to 81131a70a4)