dotnet/wpf · error · ArgumentException

SR.ThemeMode value is invalid. Use None, System, Light or…

Error message

SR.ThemeMode value {0} is invalid. Use None, System, Light or Dark

What it means

The Window.ThemeMode property setter validates the requested theme value against ThemeManager.IsValidThemeMode. Passing anything other than None, System, Light, or Dark throws an ArgumentException with the message "ThemeMode value {0} is invalid. Use None, System, Light or Dark".

Solutions

  1. Set ThemeMode to exactly one of None, System, Light, Dark
  2. Validate/sanitize the persisted theme setting before assigning it
  3. Parse user/config input into a strongly typed enum with TryParse before applying
  4. Add app-level fallback to System for unrecognized values

Example fix

// before
window.ThemeMode = config["theme"]; // "midnight" -> ArgumentException
// after
var mode = Enum.TryParse(config["theme"], true, out ThemeMode m) ? m : ThemeMode.System;
window.ThemeMode = mode;
Defensive patterns

Strategy: validation

Validate before calling

string[] valid = { "None", "System", "Light", "Dark" };
if (!valid.Contains(themeValue)) themeValue = "System";

Type guard

bool IsValidThemeMode(string s) => s is "None" or "System" or "Light" or "Dark";

Try / catch

try { window.ThemeMode = value; } catch (ArgumentException ex) when (ex.Message.Contains("ThemeMode")) { window.ThemeMode = "System"; }

Prevention

When it happens

Trigger: Setting window.ThemeMode (in code or XAML) to a string/value not among None, System, Light, Dark - e.g. a typo like "Darkd", "dark" with different casing if not accepted, or a bound value from configuration.

Common situations: Loading the theme from a settings file or user preference that contains an unsupported name; typos in XAML attributes; binding ThemeMode to a free-form string property.

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/3573337d600bbd8e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:586

        ///
        ///     This property is experimental and may be removed in future versions.
        /// </remarks>
        [Experimental("WPF0001")]
        [TypeConverter(typeof(ThemeModeConverter))]
        public ThemeMode ThemeMode
        {
            get
            {
                VerifyContextAndObjectState();
                return _themeMode;
            }
            set
            {
                VerifyContextAndObjectState();

                if(!ThemeManager.IsValidThemeMode(value))
                {
                    throw new ArgumentException(string.Format("ThemeMode value {0} is invalid. Use None, System, Light or Dark", value));
                }

                ThemeMode oldTheme = _themeMode;
                _themeMode = value;

                if(!AreResourcesInitialized)
                {
                    ThemeManager.OnWindowThemeChanged(this, oldTheme, value);
                    AreResourcesInitialized = false;

                    _reloadFluentDictionary = true;
                }

                if(IsSourceWindowNull)
                {
                    _deferThemeLoading = true;
                }
                else

View on GitHub (pinned to 81131a70a4)