{"record":{"id":"afe6e0b8829557b0","repo":"lepoco/wpfui","slug":"exceptionenumtobooleanconvertervaluemustbeanenum","errorCode":null,"errorMessage":"ExceptionEnumToBooleanConverterValueMustBeAnEnum","messagePattern":"ExceptionEnumToBooleanConverterValueMustBeAnEnum","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"samples/Wpf.Ui.Demo.Mvvm/Helpers/EnumToBooleanConverter.cs","lineNumber":21,"sourceCode":"// Copyright (C) Leszek Pomianowski and WPF UI Contributors.\n// All Rights Reserved.\n\nusing System.Windows.Data;\n\nnamespace Wpf.Ui.Demo.Mvvm.Helpers;\n\ninternal class EnumToBooleanConverter : IValueConverter\n{\n    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)\n    {\n        if (parameter is not string enumString)\n        {\n            throw new ArgumentException(\"ExceptionEnumToBooleanConverterParameterMustBeAnEnumName\");\n        }\n\n        if (!Enum.IsDefined(typeof(Wpf.Ui.Appearance.ApplicationTheme), value))\n        {\n            throw new ArgumentException(\"ExceptionEnumToBooleanConverterValueMustBeAnEnum\");\n        }\n\n        var enumValue = Enum.Parse(typeof(Wpf.Ui.Appearance.ApplicationTheme), enumString);\n\n        return enumValue.Equals(value);\n    }\n\n    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)\n    {\n        if (parameter is not string enumString)\n        {\n            throw new ArgumentException(\"ExceptionEnumToBooleanConverterParameterMustBeAnEnumName\");\n        }\n\n        return Enum.Parse(typeof(Wpf.Ui.Appearance.ApplicationTheme), enumString);\n    }\n}\n","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/samples/Wpf.Ui.Demo.Mvvm/Helpers/EnumToBooleanConverter.cs#L3-L39","documentation":"Thrown by EnumToBooleanConverter.Convert when the bound value is not a defined member of Wpf.Ui.Appearance.ApplicationTheme. The converter uses Enum.IsDefined to validate the input before comparing it to the parsed parameter, so any value outside the enum's declared names (including an invalid boxed value or wrong-typed binding) fails.","triggerScenarios":"The bound source property is null, an int that does not map to a defined theme, or an instance of a different enum/type than ApplicationTheme. Enum.IsDefined(typeof(ApplicationTheme), value) returns false for these, raising the ArgumentException.","commonSituations":"Binding the converter to a property of the wrong type (e.g. a custom theme enum or string), a source that initializes to null/default before being set, or a version mismatch where a theme value was removed from ApplicationTheme.","solutions":["Ensure the bound property is of type Wpf.Ui.Appearance.ApplicationTheme and is initialized to a defined value before the view loads.","If you extended ApplicationTheme with custom members, rebuild against the same Wpf.Ui version so Enum.IsDefined agrees.","Coerce/validate the bound value to a known theme in the view-model setter so the converter never receives an undefined value."],"exampleFix":"// before\npublic object CurrentTheme { get; set; } = 99; // not a defined ApplicationTheme\n\n// after\npublic ApplicationTheme CurrentTheme { get; set; } = ApplicationTheme.Light;","handlingStrategy":"validation","validationCode":"if (value is not Wpf.Ui.Appearance.ApplicationTheme theme || !Enum.IsDefined(typeof(Wpf.Ui.Appearance.ApplicationTheme), theme))\n{\n    return Binding.DoNothing;\n}","typeGuard":"static bool IsValidThemeValue(object? v) => v is Wpf.Ui.Appearance.ApplicationTheme t && Enum.IsDefined(typeof(Wpf.Ui.Appearance.ApplicationTheme), t);","tryCatchPattern":null,"preventionTips":["Type the bound source property as Wpf.Ui.Appearance.ApplicationTheme and initialize it.","Avoid binding the converter to loosely-typed or nullable sources without coercion.","Keep the Wpf.Ui version consistent between the app and any theme enum extensions."],"tags":["xaml","binding","converter","enum"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}