{"record":{"id":"37d88fe68396327d","repo":"lepoco/wpfui","slug":"nameof-value-is-not-type-typeof-tenum","errorCode":null,"errorMessage":"{nameof(value)} is not type: {typeof(TEnum)}","messagePattern":"(.+?) is not type: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Converters/EnumToBoolConverter.cs","lineNumber":17,"sourceCode":"// This Source Code Form is subject to the terms of the MIT License.\n// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.\n// Copyright (C) Leszek Pomianowski and WPF UI Contributors.\n// All Rights Reserved.\n\nusing System.Windows.Data;\n\nnamespace Wpf.Ui.Converters;\n\ninternal class EnumToBoolConverter<TEnum> : IValueConverter\n    where TEnum : Enum\n{\n    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)\n    {\n        if (value is not TEnum valueEnum)\n        {\n            throw new ArgumentException($\"{nameof(value)} is not type: {typeof(TEnum)}\");\n        }\n\n        if (parameter is not TEnum parameterEnum)\n        {\n            throw new ArgumentException($\"{nameof(parameter)} is not type: {typeof(TEnum)}\");\n        }\n\n        return EqualityComparer<TEnum>.Default.Equals(valueEnum, parameterEnum);\n    }\n\n    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)\n    {\n        throw new NotImplementedException();\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Converters/EnumToBoolConverter.cs#L1-L33","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Bind the converter only to properties of the exact TEnum type.","If the source is an int, add a value converter that casts to the enum first, or change the source property type.","Handle null/nullable sources by ensuring the property has a default enum value."],"exampleFix":"<!-- before: DefaultButton exposed as int -->\n<CheckBox IsChecked=\"{Binding Mode, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:Mode.Simple}}\" />\n\n<!-- after: bind a typed enum property -->\n<CheckBox IsChecked=\"{Binding ModeEnum, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:Mode.Simple}}\" />","handlingStrategy":"type-guard","validationCode":"if (value is not TEnum)\n{\n    throw new ArgumentException($\"Bound value must be {typeof(TEnum)}.\", nameof(value));\n}","typeGuard":"static bool IsCorrectEnumType(object? value) => value is TEnum;","tryCatchPattern":"try { return converter.Convert(value, targetType, parameter, culture); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"is not type\"))\n{\n    _logger.LogError(ex, \"EnumToBoolConverter bound to wrong value type\");\n    return DependencyProperty.UnsetValue;\n}","preventionTips":["Bind EnumToBoolConverter only to properties of the exact enum type.","Convert int/string sources to the enum before binding.","Default nullable enum source properties to a defined value."],"tags":["converter","enum","binding","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}