dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException(param, edge, ElementEdge)

Error message

InvalidEnumArgumentException(param, edge, ElementEdge)

What it means

ValidationHelper.VerifyElementEdge throws InvalidEnumArgumentException when the supplied ElementEdge is not one of BeforeStart, AfterStart, BeforeEnd, AfterEnd. ElementEdge is a four-value enum; any other value is an invalid enum argument.

Solutions

  1. Pass one of the four valid ElementEdge members.
  2. Validate raw ints with Enum.IsDefined(typeof(ElementEdge), value) before casting.
  3. Fix interop/marshalling code that misnumbers the enum.

Example fix

// before
var edge = (ElementEdge)savedInt;
// after
if (!Enum.IsDefined(typeof(ElementEdge), savedInt)) throw new InvalidDataException("bad edge");
var edge = (ElementEdge)savedInt;
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(ElementEdge), value)) throw new InvalidEnumArgumentException(nameof(value), value, typeof(ElementEdge));

Type guard

bool IsValidElementEdge(object v) => v is ElementEdge e && (e == ElementEdge.BeforeStart || e == ElementEdge.AfterStart || e == ElementEdge.BeforeEnd || e == ElementEdge.AfterEnd);

Try / catch

try { ... } catch (InvalidEnumArgumentException ex) { /* default to a valid edge */ }

Prevention

When it happens

Trigger: Passing an ElementEdge produced by an unchecked int cast or bad data to APIs taking a 'param' element edge, e.g. TextElement positioning helpers.

Common situations: Restoring persisted insertion-edge values from disk or interop without validation; mixing up a different WPF enum with ElementEdge.

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/09c5065b9ff488c7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs:85

        // 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 Validation
        //
        // ...............................................................

        // Checks whether it is valid to insert the child object at passed position.
        internal static void ValidateChild(TextPointer position, object child, string paramName)
        {
            Invariant.Assert(position != null);

            if (child == null)
            {
                throw new ArgumentNullException(paramName);
            }

View on GitHub (pinned to 81131a70a4)