dotnet/wpf · error · InvalidEnumArgumentException
SR.ValueInvalidForEnum
Error message
SR.ValueInvalidForEnum
What it means
The Window.WindowStartupLocation property setter validates the assigned value against the WindowStartupLocation enum (Manual, CenterScreen, CenterOwner). Passing an out-of-range integer or invalid value throws InvalidEnumArgumentException (SR.ValueInvalidForEnum) for parameter 'value'.
Solutions
- Use enum constants (WindowStartupLocation.CenterScreen) instead of raw casts
- Validate with Enum.IsDefined before casting stored integers
- Clamp or fall back to Manual when a persisted value is out of range
- Parse with Enum.TryParse<T> with a defined fallback
Example fix
// before
WindowStartupLocation = (WindowStartupLocation)settings.StartupLoc; // 7 -> throws
// after
WindowStartupLocation = Enum.IsDefined(typeof(WindowStartupLocation), settings.StartupLoc)
? (WindowStartupLocation)settings.StartupLoc
: WindowStartupLocation.Manual; Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(WindowStartupLocation), rawValue)) rawValue = (int)WindowStartupLocation.Manual;
Type guard
bool IsValidStartupLocation(int v) => Enum.IsDefined(typeof(WindowStartupLocation), v);
Try / catch
try { window.WindowStartupLocation = loc; } catch (InvalidEnumArgumentException) { window.WindowStartupLocation = WindowStartupLocation.Manual; } Prevention
- Use enum members, never raw int casts
- Validate deserialized enum values with Enum.IsDefined
- Store enums as strings in settings files
When it happens
Trigger: Assigning WindowStartupLocation = (WindowStartupLocation)castInt where the int is not 0/1/2, deserializing a numeric enum from config with an invalid value, or data-binding a raw integer to the property.
Common situations: Persisted settings files containing an invalid numeric value; interop or reflection code casting unvalidated ints; hand-edited XAML resource values.
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…
- SR.Storyboard_UnrecognizedHandoffBehavior
- SR.ValidationRule_UnknownStep (formatted with…
- ThemeMode value is invalid. Use None, System, Light or Dark
- Animation_UnrecognizedHandoffBehavior
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/606cd6824d0bbf0f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:1120
// this call ends up throwing an exception if accessing
// WindowStartupLocation is not allowed
VerifyApiSupported();
return _windowStartupLocation;
}
set
{
VerifyContextAndObjectState();
// this call ends up throwing an exception if accessing
// WindowStartupLocation is not allowed
VerifyApiSupported();
//validate WindowStartupLocation enum
if (!IsValidWindowStartupLocation(value))
{
throw new InvalidEnumArgumentException("value", (int)value, typeof( WindowStartupLocation ));
}
_windowStartupLocation = value;
}
}
/// <summary>
/// The DependencyProperty for ShowInTaskbarProperty.
/// Flags: None
/// Default Value: true
/// </summary>
public static readonly DependencyProperty ShowInTaskbarProperty =
DependencyProperty.Register("ShowInTaskbar",
typeof(bool),
typeof(Window),
new FrameworkPropertyMetadata(BooleanBoxes.TrueBox,
new PropertyChangedCallback(_OnShowInTaskbarChanged),
new CoerceValueCallback(VerifyAccessCoercion)));
View on GitHub (pinned to 81131a70a4)