{"record":{"id":"a10a0ec93725c53c","repo":"dotnet/wpf","slug":"thememode-value-0-is-invalid-use-none-system-light-or-dark","errorCode":null,"errorMessage":"ThemeMode value {0} is invalid. Use None, System, Light or Dark","messagePattern":"ThemeMode value (.+?) is invalid\\. Use None, System, Light or Dark","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":971,"sourceCode":"        ///     in the application resources manually. If you do, the Fluent theme dictionaries added in the application\n        ///     resources will take precedence over the ones added by setting this property.\n        ///     \n        ///     This property is experimental and may be removed in future versions.\n        /// </remarks>\n        [Experimental(\"WPF0001\")]\n        [TypeConverter(typeof(ThemeModeConverter))]\n        public ThemeMode ThemeMode\n        {\n            get\n            {\n                return _themeMode;\n            }\n            set\n            {\n                VerifyAccess();\n                if (!ThemeManager.IsValidThemeMode(value))\n                {\n                    throw new ArgumentException(string.Format(\"ThemeMode value {0} is invalid. Use None, System, Light or Dark\", value));\n                }\n                \n                ThemeMode oldValue = _themeMode;\n                _themeMode = value;\n\n                if(!_resourcesInitialized)\n                {\n\n                    ThemeManager.OnApplicationThemeChanged(oldValue, value);\n\n                    // If the resources are not initializd, fluent dictionary\n                    // included in this operation will be reset.\n                    // Hence, we need to reload the fluent dictionary.\n                    _reloadFluentDictionary = true;\n\n                    // OnApplicationThemeChanged will trigger InvalidateResourceReferences\n                    // which will mark _resourcesInitialized = true, however since \n                    // the value earlier was false, it means that Resources may not have been","sourceCodeStart":953,"sourceCodeEnd":989,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L953-L989","documentation":"The Application.ThemeMode setter only accepts None, System, Light, or Dark. ThemeManager.IsValidThemeMode validates the value; anything else throws ArgumentException with a message naming the invalid value and the allowed options. This guards the theme manager from undefined theme states.","triggerScenarios":"Assigning app.ThemeMode = (ThemeMode)rawInt from config or user input where the integer is not one of the four defined members; binding a string-typed setting directly to the property; passing a custom/unmapped enum value.","commonSituations":"User preferences stored as integers in settings files; two-way bindings delivering invalid values; theme enums extended or renamed between runtime versions.","solutions":["Validate with Enum.IsDefined(typeof(ThemeMode), value) and map unknown values to ThemeMode.System","Only assign the named members None, System, Light, Dark","Coerce config/settings values through a TryParse-style mapper before assignment","Set the property on the UI thread — the setter calls VerifyAccess"],"exampleFix":"// before\napp.ThemeMode = (ThemeMode)settings.ThemeValue;\n// after\nvar mode = Enum.IsDefined(typeof(ThemeMode), settings.ThemeValue) ? (ThemeMode)settings.ThemeValue : ThemeMode.System;\napp.ThemeMode = mode;","handlingStrategy":"validation","validationCode":"bool isValidTheme(int v) => Enum.IsDefined(typeof(ThemeMode), v) && (ThemeMode)v is ThemeMode.None or ThemeMode.System or ThemeMode.Light or ThemeMode.Dark;","typeGuard":"ThemeMode NormalizeTheme(int raw) => Enum.IsDefined(typeof(ThemeMode), raw) ? (ThemeMode)raw : ThemeMode.System;","tryCatchPattern":"try { app.ThemeMode = mode; } catch (ArgumentException) { app.ThemeMode = ThemeMode.System; }","preventionTips":["Validate settings-backed integers before binding","Map unknown themes to System","Assign on the UI thread","Keep bindings constrained to the four valid members"],"tags":["wpf","theming","enum","invalid-enum-argument","validation"],"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"}