{"record":{"id":"c488c67c64d91aa6","repo":"MahApps/MahApps.Metro","slug":"the-enumtype-must-be-specified","errorCode":null,"errorMessage":"The EnumType must be specified.","messagePattern":"The EnumType must be specified\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro.Samples/MahApps.Metro.Demo/Markup/EnumBindingSourceExtension.cs","lineNumber":66,"sourceCode":"        public EnumBindingSourceExtension()\n        {\n        }\n\n        /// <summary>\n        /// Initializes a new instance of EnumBindingSourceExtension.\n        /// </summary>\n        /// <param name=\"enumType\">The type of the Enum.</param>\n        public EnumBindingSourceExtension(Type enumType)\n        {\n            this.EnumType = enumType;\n        }\n\n        /// <inheritdoc />\n        public override object ProvideValue(IServiceProvider serviceProvider)\n        {\n            if (this.EnumType is null)\n            {\n                throw new InvalidOperationException(\"The EnumType must be specified.\");\n            }\n\n            var underlyingEnumType = Nullable.GetUnderlyingType(this.EnumType) ?? this.EnumType;\n            var enumValues = Enum.GetValues(underlyingEnumType);\n\n            if (underlyingEnumType == this.EnumType)\n            {\n                return enumValues;\n            }\n\n            var nullableEnumValues = Array.CreateInstance(underlyingEnumType, enumValues.Length + 1);\n            enumValues.CopyTo(nullableEnumValues, 1);\n            return nullableEnumValues;\n        }\n    }\n}","sourceCodeStart":48,"sourceCodeEnd":82,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro.Samples/MahApps.Metro.Demo/Markup/EnumBindingSourceExtension.cs#L48-L82","documentation":"ProvideValue is the WPF markup-extension entry point called when the binding is evaluated. If EnumType was never set (the parameterless constructor was used and no EnumType assignment followed), ProvideValue throws InvalidOperationException telling you the EnumType must be specified before the extension can produce values.","triggerScenarios":"Using EnumBindingSourceExtension in XAML without specifying EnumType (e.g. <ItemsControl ItemsSource=\"{local:EnumBindingSource}\"/> with no EnumType assignment), or constructing it in code with the parameterless ctor and calling ProvideValue without setting EnumType.","commonSituations":"XAML author forgot the EnumType attribute. A style/template resource uses the extension with the default constructor only. Refactoring removed the EnumType assignment.","solutions":["Supply EnumType in XAML: {local:EnumBindingSource EnumType={x:Type local:MyEnum}}.","Or use the parameterized constructor: {local:EnumBindingSource {x:Type local:MyEnum}}.","If constructed in code, set EnumType before ProvideValue / before the binding resolves."],"exampleFix":"<!-- before -->\n<ComboBox ItemsSource=\"{local:EnumBindingSource}\"/>\n<!-- after -->\n<ComboBox ItemsSource=\"{local:EnumBindingSource EnumType={x:Type local:MyEnum}}\"/>","handlingStrategy":"validation","validationCode":"// Always specify EnumType, either via ctor or property\nvar ext = new EnumBindingSourceExtension(typeof(MyEnum)); // ctor sets EnumType\n// or in XAML: {local:EnumBindingSource EnumType={x:Type local:MyEnum}}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the enum type — prefer the EnumBindingSourceExtension(Type) constructor.","In XAML, set EnumType explicitly; never rely on the parameterless constructor alone.","Compile-and-run a quick binding test to catch missing EnumType at design/runtime early.","Document the extension with a complete usage example."],"tags":["wpf","xaml","markup-extension","enum","binding"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}