{"record":{"id":"3dbcf6e48bb0cc45","repo":"MahApps/MahApps.Metro","slug":"type-must-be-for-an-enum","errorCode":null,"errorMessage":"Type must be for an Enum.","messagePattern":"Type must be for an Enum\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro.Samples/MahApps.Metro.Demo/Markup/EnumBindingSourceExtension.cs","lineNumber":36,"sourceCode":"        /// <summary>\n        /// Gets or sets the type of the Enum.\n        /// </summary>\n        /// <exception cref=\"ArgumentException\">Value is not an Enum type.</exception>\n        [ConstructorArgument(\"enumType\")]\n        public Type? EnumType\n        {\n            get => this.enumType;\n            set\n            {\n                if (value != this.enumType)\n                {\n                    if (null != value)\n                    {\n                        var type = Nullable.GetUnderlyingType(value) ?? value;\n\n                        if (!type.IsEnum)\n                        {\n                            throw new ArgumentException(\"Type must be for an Enum.\");\n                        }\n                    }\n\n                    this.enumType = value;\n                }\n            }\n        }\n\n        /// <summary>\n        /// Initializes a new instance of EnumBindingSourceExtension.\n        /// </summary>\n        public EnumBindingSourceExtension()\n        {\n        }\n\n        /// <summary>\n        /// Initializes a new instance of EnumBindingSourceExtension.\n        /// </summary>","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro.Samples/MahApps.Metro.Demo/Markup/EnumBindingSourceExtension.cs#L18-L54","documentation":"EnumBindingSourceExtension is a XAML markup extension that exposes an enum's values for binding. Its EnumType property setter validates that the assigned Type is (or is a Nullable<> of) an enum; if not it throws ArgumentException. The setter handles the nullable case via Nullable.GetUnderlyingType before the IsEnum check.","triggerScenarios":"Setting EnumType to a non-enum Type — e.g. {x:Type local:MyStruct}, {x:Type sys:Int32} via XAML, or assigning programmatically with a class/struct/int Type. The null/Nullable<int> wrapped check still rejects it because the underlying type is not an enum.","commonSituations":"XAML mistakenly binds the extension to a class, struct, or primitive instead of an enum. Refactoring a type from enum to something else without updating the XAML. Copy-paste of the markup extension to a non-enum source.","solutions":["Set EnumType to an actual enum type, e.g. {x:Type local:MyEnum} or in code typeof(MyEnum).","If you need nullable enum binding, use Nullable<MyEnum> (e.g. {x:Type sys:Nullable`1[local:MyEnum]}) — the setter already unwraps Nullable.","Fix the XAML that points to the wrong type; the design-time parser will catch the ArgumentException immediately."],"exampleFix":"<!-- before -->\n<ComboBox ItemsSource=\"{markup:EnumBindingSource EnumType={x:Type local:MySettings}}\"/>\n<!-- after -->\n<ComboBox ItemsSource=\"{markup:EnumBindingSource EnumType={x:Type local:MyEnum}}\"/>","handlingStrategy":"type-guard","validationCode":"// Validate the type is an enum (or Nullable<enum>) before assigning\nstatic bool IsEnumType(Type t) {\n    if (t is null) return true; // null allowed by the setter\n    var underlying = Nullable.GetUnderlyingType(t) ?? t;\n    return underlying.IsEnum;\n}\nif (!IsEnumType(myType)) throw new ArgumentException($\"{myType} is not an enum\");\next.EnumType = myType;","typeGuard":"static bool IsEnumOrNullableEnum(Type? t) {\n    if (t is null) return false;\n    var underlying = Nullable.GetUnderlyingType(t) ?? t;\n    return underlying.IsEnum;\n}","tryCatchPattern":null,"preventionTips":["In XAML, double-check that EnumType points to an enum (look for `enum` keyword in the referenced type).","When binding Nullable enums, ensure the underlying type is an enum, not a primitive.","Validate the Type at design time (the XAML designer surfaces the ArgumentException immediately).","Keep EnumBindingSourceExtension usage documented with an enum-only example."],"tags":["wpf","xaml","markup-extension","enum","binding","validation"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}