lepoco/wpfui · error · ArgumentException

ExceptionEnumToBooleanConverterValueMustBeAnEnum

Error message

ExceptionEnumToBooleanConverterValueMustBeAnEnum

What it means

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.

Source

Thrown at samples/Wpf.Ui.Demo.Mvvm/Helpers/EnumToBooleanConverter.cs:21

// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows.Data;

namespace Wpf.Ui.Demo.Mvvm.Helpers;

internal class EnumToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter is not string enumString)
        {
            throw new ArgumentException("ExceptionEnumToBooleanConverterParameterMustBeAnEnumName");
        }

        if (!Enum.IsDefined(typeof(Wpf.Ui.Appearance.ApplicationTheme), value))
        {
            throw new ArgumentException("ExceptionEnumToBooleanConverterValueMustBeAnEnum");
        }

        var enumValue = Enum.Parse(typeof(Wpf.Ui.Appearance.ApplicationTheme), enumString);

        return enumValue.Equals(value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter is not string enumString)
        {
            throw new ArgumentException("ExceptionEnumToBooleanConverterParameterMustBeAnEnumName");
        }

        return Enum.Parse(typeof(Wpf.Ui.Appearance.ApplicationTheme), enumString);
    }
}

View on GitHub (pinned to ffebacd610)

Solutions

  1. Ensure the bound property is of type Wpf.Ui.Appearance.ApplicationTheme and is initialized to a defined value before the view loads.
  2. If you extended ApplicationTheme with custom members, rebuild against the same Wpf.Ui version so Enum.IsDefined agrees.
  3. Coerce/validate the bound value to a known theme in the view-model setter so the converter never receives an undefined value.

Example fix

// before
public object CurrentTheme { get; set; } = 99; // not a defined ApplicationTheme

// after
public ApplicationTheme CurrentTheme { get; set; } = ApplicationTheme.Light;
Defensive patterns

Strategy: validation

Validate before calling

if (value is not Wpf.Ui.Appearance.ApplicationTheme theme || !Enum.IsDefined(typeof(Wpf.Ui.Appearance.ApplicationTheme), theme))
{
    return Binding.DoNothing;
}

Type guard

static bool IsValidThemeValue(object? v) => v is Wpf.Ui.Appearance.ApplicationTheme t && Enum.IsDefined(typeof(Wpf.Ui.Appearance.ApplicationTheme), t);

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/afe6e0b8829557b0. Report an issue: GitHub.