{"record":{"id":"44be51f9fcf65765","repo":"dotnet/wpf","slug":"invalidenumargumentexception-targets-int-targets-typeof","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"targets\", (int)targets, typeof(RawUIStateTargets))","messagePattern":"InvalidEnumArgumentException\\(\"targets\", \\(int\\)targets, typeof\\(RawUIStateTargets\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawUIStateInputReport.cs","lineNumber":40,"sourceCode":"        ///     The time when the input occurred.\n        /// </param>\n        /// <param name=\"action\">\n        ///     The action being reported.\n        /// </param>\n        /// <param name=\"targets\">\n        ///     The targets being reported.\n        /// </param>\n        public RawUIStateInputReport(\n            PresentationSource inputSource,\n            InputMode mode,\n            int timestamp,\n            RawUIStateActions action,\n            RawUIStateTargets targets) : base(inputSource, InputType.Keyboard, mode, timestamp)\n        {\n            if (!IsValidRawUIStateAction(action))\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"action\", (int)action, typeof(RawUIStateActions));\n            if (!IsValidRawUIStateTargets(targets))\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"targets\", (int)targets, typeof(RawUIStateTargets));\n\n            _action = action;\n            _targets = targets;\n        }\n\n        /// <summary>\n        ///     Read-only access to the action that was reported.\n        /// </summary>\n        public RawUIStateActions Action {get {return _action;}}\n\n        /// <summary>\n        ///     Read-only access to the targets that were reported.\n        /// </summary>\n        public RawUIStateTargets Targets {get {return _targets;}}\n\n        // IsValid Method for RawUIStateActions.\n        internal static bool IsValidRawUIStateAction(RawUIStateActions action)\n        {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawUIStateInputReport.cs#L22-L58","documentation":"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.","triggerScenarios":"Passing an undefined or empty RawUIStateTargets value to the RawUIStateInputReport constructor, usually via an unchecked numeric cast or an invalid flag combination.","commonSituations":"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.","solutions":["Use named RawUIStateTargets flags only (e.g. KeyboardFocus) and avoid raw numeric casts.","Validate with IsValidRawUIStateTargets/Enum.IsDefined before constructing the report.","Ensure the combination is non-empty and consists of defined members.","Rebuild against the current WPF version so enum definitions match."],"exampleFix":"// before\nvar targets = (RawUIStateTargets)0;\nvar report = new RawUIStateInputReport(src, mode, ts, action, targets); // throws\n\n// after\nvar targets = RawUIStateTargets.KeyboardFocus;\nvar report = new RawUIStateInputReport(src, mode, ts, action, targets);","handlingStrategy":"validation","validationCode":"static bool IsValidRawUIStateTargetsValue(RawUIStateTargets t) =>\n    t != 0 && Enum.IsDefined(typeof(RawUIStateTargets), t);","typeGuard":"bool TryGetUIStateTargets(int raw, out RawUIStateTargets targets)\n{\n    targets = (RawUIStateTargets)raw;\n    return targets != 0 && Enum.IsDefined(typeof(RawUIStateTargets), targets);\n}","tryCatchPattern":"try\n{\n    var report = new RawUIStateInputReport(src, mode, ts, action, targets);\n}\ncatch (InvalidEnumArgumentException ex)\n{\n    Log($\"Invalid RawUIStateTargets: {ex}\");\n}","preventionTips":["Use named RawUIStateTargets flags, never numeric casts.","Ensure the targets value is non-empty and composed of defined members.","Validate before constructing, not after catching.","Keep enum definitions in sync with the WPF version in use."],"tags":["wpf","input","enum","argument-validation"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}