MahApps/MahApps.Metro · error · InvalidOperationException
The EnumType must be specified.
Error message
The EnumType must be specified.
What it means
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.
Source
Thrown at src/MahApps.Metro.Samples/MahApps.Metro.Demo/Markup/EnumBindingSourceExtension.cs:66
public EnumBindingSourceExtension()
{
}
/// <summary>
/// Initializes a new instance of EnumBindingSourceExtension.
/// </summary>
/// <param name="enumType">The type of the Enum.</param>
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
/// <inheritdoc />
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.EnumType is null)
{
throw new InvalidOperationException("The EnumType must be specified.");
}
var underlyingEnumType = Nullable.GetUnderlyingType(this.EnumType) ?? this.EnumType;
var enumValues = Enum.GetValues(underlyingEnumType);
if (underlyingEnumType == this.EnumType)
{
return enumValues;
}
var nullableEnumValues = Array.CreateInstance(underlyingEnumType, enumValues.Length + 1);
enumValues.CopyTo(nullableEnumValues, 1);
return nullableEnumValues;
}
}
}View on GitHub (pinned to 72099e310b)
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.
Example fix
<!-- before -->
<ComboBox ItemsSource="{local:EnumBindingSource}"/>
<!-- after -->
<ComboBox ItemsSource="{local:EnumBindingSource EnumType={x:Type local:MyEnum}}"/> Defensive patterns
Strategy: validation
Validate before calling
// Always specify EnumType, either via ctor or property
var ext = new EnumBindingSourceExtension(typeof(MyEnum)); // ctor sets EnumType
// or in XAML: {local:EnumBindingSource EnumType={x:Type local:MyEnum}} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: XAML author forgot the EnumType attribute. A style/template resource uses the extension with the default constructor only. Refactoring removed the EnumType assignment.
Related errors
- Type must be for an Enum.
- Cannot convert the given input to a valid color
- Uups, it seems like there is something wrong with the given
- Unable to find the dialog closing storyboard. Did you forget
- Another DataGrid is already attached to this {nameof(DataGri
AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13).
Data as JSON: /api/errors/c488c67c64d91aa6.
Report an issue: GitHub.