dotnet/wpf · error · InvalidEnumArgumentException

value

Error message

value

What it means

Setting FrameworkContentElement.InheritanceBehavior with a value outside the defined InheritanceBehavior enum range throws InvalidEnumArgumentException naming the 'value' parameter. The setter validates the raw integer before storing it in internal flag bits, because the enum is packed into a bit field that only supports its defined range (Default through SkipAllNext).

Solutions

  1. Validate the integer against Enum.IsDefined(typeof(InheritanceBehavior), v) before casting and assigning
  2. Fix the persisted/configured numeric value to a defined enum member
  3. Use enum.TryParse with the string name instead of raw integer casts

Example fix

// before
var v = (InheritanceBehavior)intFromConfig;
element.InheritanceBehavior = v; // throws for out-of-range ints

// after
if (Enum.IsDefined(typeof(InheritanceBehavior), intFromConfig))
    element.InheritanceBehavior = (InheritanceBehavior)intFromConfig;
else
    element.InheritanceBehavior = InheritanceBehavior.Default;
Defensive patterns

Strategy: validation

Validate before calling

int raw = intFromConfig;
if (!Enum.IsDefined(typeof(InheritanceBehavior), raw))
    throw new ArgumentOutOfRangeException(nameof(raw), $"{raw} is not a valid InheritanceBehavior");
element.InheritanceBehavior = (InheritanceBehavior)raw;

Type guard

static bool IsValidInheritanceBehavior(int v) =>
    v >= 0 && v <= (int)InheritanceBehavior.SkipAllNext;

Try / catch

try { element.InheritanceBehavior = (InheritanceBehavior)raw; }
catch (InvalidEnumArgumentException ex) { log.Error("Bad InheritanceBehavior value", ex); }

Prevention

When it happens

Trigger: Assigning InheritanceBehavior = (InheritanceBehavior)someRawInt where the cast integer is negative or greater than (int)InheritanceBehavior.SkipAllNext.

Common situations: Persisting/reading the enum as an int from config, XAML, or serialization where a stale or hand-edited numeric value is cast blindly; off-by-one constants from another enum family.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs:1490

            }

            set
            {
                Debug.Assert((uint)InternalFlags.InheritanceBehavior0 == 0x08);
                Debug.Assert((uint)InternalFlags.InheritanceBehavior1 == 0x10);
                Debug.Assert((uint)InternalFlags.InheritanceBehavior2 == 0x20);

                const uint inheritanceBehaviorMask =
                    (uint)InternalFlags.InheritanceBehavior0 +
                    (uint)InternalFlags.InheritanceBehavior1 +
                    (uint)InternalFlags.InheritanceBehavior2;

                if (!this.IsInitialized)
                {
                    if ((uint)value < 0 ||
                        (uint)value > (uint)InheritanceBehavior.SkipAllNext)
                    {
                        throw new InvalidEnumArgumentException("value", (int)value, typeof(InheritanceBehavior));
                    }

                    uint inheritanceBehavior = (uint)value << 3;

                    _flags = (InternalFlags)((inheritanceBehavior & inheritanceBehaviorMask) | (((uint)_flags) & ~inheritanceBehaviorMask));

                    if (_parent != null)
                    {
                        // This means that we are in the process of xaml parsing:
                        // an instance of FCE has been created and added to a parent,
                        // but no children yet added to it (otherwise it would be initialized already
                        // and we would not be allowed to change InheritanceBehavior).
                        // So we need to re-calculate properties accounting for the new
                        // inheritance behavior.
                        // This must have no performance effect as the subtree of this
                        // element is empty (no children yet added).
                        TreeWalkHelper.InvalidateOnTreeChange(/*fe:*/null, /*fce:*/this, _parent, true);
                    }

View on GitHub (pinned to 81131a70a4)