dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException("targets", (int)targets…

Error message

InvalidEnumArgumentException("targets", (int)targets, typeof(RawUIStateTargets))

What it means

The same constructor validates targets with IsValidRawUIStateTargets and throws InvalidEnumArgumentException when the value is not a valid RawUIStateTargets combination. Targets indicate which UI-state elements the report applies to, and undefined values are rejected.

Solutions

  1. Use named RawUIStateTargets flags only (e.g. KeyboardFocus) and avoid raw numeric casts.
  2. Validate with IsValidRawUIStateTargets/Enum.IsDefined before constructing the report.
  3. Ensure the combination is non-empty and consists of defined members.
  4. Rebuild against the current WPF version so enum definitions match.

Example fix

// before
var targets = (RawUIStateTargets)0;
var report = new RawUIStateInputReport(src, mode, ts, action, targets); // throws

// after
var targets = RawUIStateTargets.KeyboardFocus;
var report = new RawUIStateInputReport(src, mode, ts, action, targets);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidRawUIStateTargetsValue(RawUIStateTargets t) =>
    t != 0 && Enum.IsDefined(typeof(RawUIStateTargets), t);

Type guard

bool TryGetUIStateTargets(int raw, out RawUIStateTargets targets)
{
    targets = (RawUIStateTargets)raw;
    return targets != 0 && Enum.IsDefined(typeof(RawUIStateTargets), targets);
}

Try / catch

try
{
    var report = new RawUIStateInputReport(src, mode, ts, action, targets);
}
catch (InvalidEnumArgumentException ex)
{
    Log($"Invalid RawUIStateTargets: {ex}");
}

Prevention

When it happens

Trigger: Passing an undefined or empty RawUIStateTargets value to the RawUIStateInputReport constructor, usually via an unchecked numeric cast or an invalid flag combination.

Common situations: Same family as the action error: synthesized input reports in test/automation code, deserialized legacy integers, or hand-combined flag values that no longer match the enum definition.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawUIStateInputReport.cs:40

        ///     The time when the input occurred.
        /// </param>
        /// <param name="action">
        ///     The action being reported.
        /// </param>
        /// <param name="targets">
        ///     The targets being reported.
        /// </param>
        public RawUIStateInputReport(
            PresentationSource inputSource,
            InputMode mode,
            int timestamp,
            RawUIStateActions action,
            RawUIStateTargets targets) : base(inputSource, InputType.Keyboard, mode, timestamp)
        {
            if (!IsValidRawUIStateAction(action))
                throw new System.ComponentModel.InvalidEnumArgumentException("action", (int)action, typeof(RawUIStateActions));
            if (!IsValidRawUIStateTargets(targets))
                throw new System.ComponentModel.InvalidEnumArgumentException("targets", (int)targets, typeof(RawUIStateTargets));

            _action = action;
            _targets = targets;
        }

        /// <summary>
        ///     Read-only access to the action that was reported.
        /// </summary>
        public RawUIStateActions Action {get {return _action;}}

        /// <summary>
        ///     Read-only access to the targets that were reported.
        /// </summary>
        public RawUIStateTargets Targets {get {return _targets;}}

        // IsValid Method for RawUIStateActions.
        internal static bool IsValidRawUIStateAction(RawUIStateActions action)
        {

View on GitHub (pinned to 81131a70a4)