dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException("value", (int) value…
Error message
InvalidEnumArgumentException("value", (int) value, typeof(BindingMode)) What it means
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.
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.
Example fix
// before binding.Mode = (BindingMode)rawInt; // after binding.Mode = Enum.IsDefined(typeof(BindingMode), rawInt) ? (BindingMode)rawInt : BindingMode.Default;
Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(BindingMode), value))
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(BindingMode));
binding.Mode = (BindingMode)value; Type guard
static bool IsValidBindingMode(int v) => Enum.IsDefined(typeof(BindingMode), v);
Try / catch
try { binding.Mode = (BindingMode)stored; } catch (InvalidEnumArgumentException) { binding.Mode = BindingMode.Default; } Prevention
- 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.
When it happens
Trigger: binding.Mode = (BindingMode)99; enum values from deserialized config/data that exceed the BindingMode range; corrupted stored enum values assigned in code.
Common situations: 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.
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
- InvalidEnumArgumentException("value", (int) value…
- category
- InvalidEnumArgumentException: routingStrategy
- Specified InkCanvasClipboardFormat is not valid.
- SR.Image_SizeOptionsAngle
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/21ed1734d5073d7f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs:363
{
switch (GetFlagsWithinMask(BindingFlags.PropagationMask))
{
case BindingFlags.OneWay: return BindingMode.OneWay;
case BindingFlags.TwoWay: return BindingMode.TwoWay;
case BindingFlags.OneWayToSource: return BindingMode.OneWayToSource;
case BindingFlags.OneTime: return BindingMode.OneTime;
case BindingFlags.PropDefault: return BindingMode.Default;
}
Invariant.Assert(false, "Unexpected BindingMode value");
return 0;
}
set
{
CheckSealed();
BindingFlags flags = FlagsFrom(value);
if (flags == BindingFlags.IllegalInput)
throw new InvalidEnumArgumentException("value", (int) value, typeof(BindingMode));
ChangeFlagsWithinMask(BindingFlags.PropagationMask, flags);
}
}
/// <summary> Update type </summary>
[DefaultValue(UpdateSourceTrigger.Default)]
public UpdateSourceTrigger UpdateSourceTrigger
{
get
{
switch (GetFlagsWithinMask(BindingFlags.UpdateMask))
{
case BindingFlags.UpdateOnPropertyChanged: return UpdateSourceTrigger.PropertyChanged;
case BindingFlags.UpdateOnLostFocus: return UpdateSourceTrigger.LostFocus;
case BindingFlags.UpdateExplicitly: return UpdateSourceTrigger.Explicit;
case BindingFlags.UpdateDefault: return UpdateSourceTrigger.Default;
}View on GitHub (pinned to 81131a70a4)