lepoco/wpfui · error · ArgumentException

{nameof(value)} is not type: {typeof(TEnum)}

Error message

{nameof(value)} is not type: {typeof(TEnum)}

What it means

EnumToBoolConverter<TEnum>.Convert requires the bound value to be of the converter's generic enum type TEnum. It throws ArgumentException when value is not assignable to TEnum. The converter compares a bound enum (e.g. ContentDialogButton) against a ConverterParameter enum for equality, so the bound value's type must match exactly.

Source

Thrown at src/Wpf.Ui/Converters/EnumToBoolConverter.cs:17

// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Windows.Data;

namespace Wpf.Ui.Converters;

internal class EnumToBoolConverter<TEnum> : IValueConverter
    where TEnum : Enum
{
    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is not TEnum valueEnum)
        {
            throw new ArgumentException($"{nameof(value)} is not type: {typeof(TEnum)}");
        }

        if (parameter is not TEnum parameterEnum)
        {
            throw new ArgumentException($"{nameof(parameter)} is not type: {typeof(TEnum)}");
        }

        return EqualityComparer<TEnum>.Default.Equals(valueEnum, parameterEnum);
    }

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

View on GitHub (pinned to ffebacd610)

Solutions

  1. Bind the converter only to properties of the exact TEnum type.
  2. If the source is an int, add a value converter that casts to the enum first, or change the source property type.
  3. Handle null/nullable sources by ensuring the property has a default enum value.

Example fix

<!-- before: DefaultButton exposed as int -->
<CheckBox IsChecked="{Binding Mode, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:Mode.Simple}}" />

<!-- after: bind a typed enum property -->
<CheckBox IsChecked="{Binding ModeEnum, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:Mode.Simple}}" />
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not TEnum)
{
    throw new ArgumentException($"Bound value must be {typeof(TEnum)}.", nameof(value));
}

Type guard

static bool IsCorrectEnumType(object? value) => value is TEnum;

Try / catch

try { return converter.Convert(value, targetType, parameter, culture); }
catch (ArgumentException ex) when (ex.Message.Contains("is not type"))
{
    _logger.LogError(ex, "EnumToBoolConverter bound to wrong value type");
    return DependencyProperty.UnsetValue;
}

Prevention

When it happens

Trigger: Binding the converter to a property whose type is not TEnum - e.g. an int, a string, an object, or a different enum - while the converter is generic over ContentDialogButton.

Common situations: Binding DefaultButton-like properties where the source exposes an int or nullable enum; binding a wrong enum type; null value when the source property is not initialised.

Related errors


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