{"record":{"id":"21ed1734d5073d7f","repo":"dotnet/wpf","slug":"invalidenumargumentexception-value-int-value-typeof-binding","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"value\", (int) value, typeof(BindingMode))","messagePattern":"InvalidEnumArgumentException\\(\"value\", \\(int\\) value, typeof\\(BindingMode\\)\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs","lineNumber":363,"sourceCode":"            {\n                switch (GetFlagsWithinMask(BindingFlags.PropagationMask))\n                {\n                    case BindingFlags.OneWay:           return BindingMode.OneWay;\n                    case BindingFlags.TwoWay:           return BindingMode.TwoWay;\n                    case BindingFlags.OneWayToSource:   return BindingMode.OneWayToSource;\n                    case BindingFlags.OneTime:          return BindingMode.OneTime;\n                    case BindingFlags.PropDefault:      return BindingMode.Default;\n                }\n                Invariant.Assert(false, \"Unexpected BindingMode value\");\n                return 0;\n            }\n            set\n            {\n                CheckSealed();\n\n                BindingFlags flags = FlagsFrom(value);\n                if (flags == BindingFlags.IllegalInput)\n                    throw new InvalidEnumArgumentException(\"value\", (int) value, typeof(BindingMode));\n\n                ChangeFlagsWithinMask(BindingFlags.PropagationMask, flags);\n            }\n        }\n\n        /// <summary> Update type </summary>\n        [DefaultValue(UpdateSourceTrigger.Default)]\n        public UpdateSourceTrigger UpdateSourceTrigger\n        {\n            get\n            {\n                switch (GetFlagsWithinMask(BindingFlags.UpdateMask))\n                {\n                    case BindingFlags.UpdateOnPropertyChanged: return UpdateSourceTrigger.PropertyChanged;\n                    case BindingFlags.UpdateOnLostFocus:    return UpdateSourceTrigger.LostFocus;\n                    case BindingFlags.UpdateExplicitly:     return UpdateSourceTrigger.Explicit;\n                    case BindingFlags.UpdateDefault:        return UpdateSourceTrigger.Default;\n                }","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs#L345-L381","documentation":"The Binding.Mode setter converts the BindingMode value via FlagsFrom; if the result is BindingFlags.IllegalInput the value is not a valid BindingMode, and an InvalidEnumArgumentException is thrown for parameter 'value'. This can also happen when an out-of-range integer is cast to the enum.","triggerScenarios":"binding.Mode = (BindingMode)99; enum values from deserialized config/data that exceed the BindingMode range; corrupted stored enum values assigned in code.","commonSituations":"Persisting enum values in settings files and casting them back unchecked; interop code receiving ints from external systems; generic data-binding frameworks assigning raw ints.","solutions":["Assign only defined BindingMode members (OneWay, TwoWay, OneWayToSource, OneTime, Default).","Validate with Enum.IsDefined(typeof(BindingMode), value) before casting/assigning.","Sanitize persisted values at load time, defaulting to BindingMode.Default."],"exampleFix":"// before\nbinding.Mode = (BindingMode)rawInt;\n// after\nbinding.Mode = Enum.IsDefined(typeof(BindingMode), rawInt) ? (BindingMode)rawInt : BindingMode.Default;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(BindingMode), value))\n    throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(BindingMode));\nbinding.Mode = (BindingMode)value;","typeGuard":"static bool IsValidBindingMode(int v) => Enum.IsDefined(typeof(BindingMode), v);","tryCatchPattern":"try { binding.Mode = (BindingMode)stored; } catch (InvalidEnumArgumentException) { binding.Mode = BindingMode.Default; }","preventionTips":["Never cast raw ints to enums without Enum.IsDefined.","Normalize persisted enum values at deserialization time.","Prefer BindingMode.Default when the stored value is unknown."],"tags":["wpf","binding","enum","argument"],"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-21T21:30:21.729Z"}