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

  1. Use enum constants (WindowStartupLocation.CenterScreen) instead of raw casts
  2. Validate with Enum.IsDefined before casting stored integers
  3. Clamp or fall back to Manual when a persisted value is out of range
  4. 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

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


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)