dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException(value, (int) value…

Error message

InvalidEnumArgumentException(value, (int) value, typeof(ListSortDirection))

What it means

The SortDescription.Direction setter validates the assigned value against the ListSortDirection enum (Ascending=0, Descending=1) and throws InvalidEnumArgumentException for any out-of-range integer cast to the enum. This catches invalid enum casts that the C# type system otherwise allows.

Solutions

  1. Validate the int is 0 or 1 before casting, or use Enum.IsDefined(typeof(ListSortDirection), value).
  2. Clamp or map the source value: value != 0 ? ListSortDirection.Descending : ListSortDirection.Ascending.
  3. Fix the source of the bad integer (config, serialization, UI binding).

Example fix

// before
sort.Direction = (ListSortDirection)rawInt; // throws for rawInt=2
// after
sort.Direction = Enum.IsDefined(typeof(ListSortDirection), rawInt) ? (ListSortDirection)rawInt : ListSortDirection.Ascending;
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(ListSortDirection), rawInt)) throw new ArgumentOutOfRangeException(nameof(rawInt));

Type guard

bool IsValidSortDirection(int v) => v == 0 || v == 1;

Try / catch

try { desc.Direction = (ListSortDirection)v; } catch (InvalidEnumArgumentException) { desc.Direction = ListSortDirection.Ascending; }

Prevention

When it happens

Trigger: Assigning (ListSortDirection)2 or any int outside 0..1 to SortDescription.Direction, typically from an int variable read from config, a database, or user input.

Common situations: Mapping a UI toggle or persisted sort-order integer to ListSortDirection without validating the raw value first.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/0e28408e398527c9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/ComponentModel/SortDescription.cs:73

                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "SortDescription"));

                _propertyName = value;
            }
        }

        /// <summary>
        /// Sort direction.
        /// </summary>
        public ListSortDirection Direction
        {
            get { return _direction; }
            set
            {
                if (_sealed)
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "SortDescription"));

                if (value < ListSortDirection.Ascending || value > ListSortDirection.Descending)
                    throw new InvalidEnumArgumentException("value", (int) value, typeof(ListSortDirection));

                _direction = value;
            }
        }

        /// <summary>
        /// Returns true if the SortDescription is in use (sealed).
        /// </summary>
        public bool IsSealed
        {
            get { return _sealed; }
        }

        #endregion Public Properties

        //------------------------------------------------------
        //
        //  Public methods

View on GitHub (pinned to 81131a70a4)