MahApps/MahApps.Metro · error · InvalidCastException
Cannot convert the given input to a valid color
Error message
Cannot convert the given input to a valid color
What it means
ColorPickerBase.OnColorNamePropertyChanged fires when the bound ColorName string changes. It asks the configured ColorHelper to parse the string; when parsing yields no color (the helper returns null), the callback throws InvalidCastException. This rejects any string the helper cannot map to a known color name or hex value.
Source
Thrown at src/MahApps.Metro/Controls/ColorPicker/ColorPickerBase.cs:139
{
colorPicker.SetCurrentValue(SelectedColorProperty, color);
}
else // if the color stayed the same we still have to update the displayed name
{
colorPicker.ColorIsUpdating = true;
try
{
colorPicker.SetCurrentValue(ColorNameProperty, (colorPicker.ColorHelper ?? ColorHelper.DefaultInstance).GetColorName(color, colorPicker.ColorNamesDictionary, colorPicker.IsAlphaChannelVisible));
}
finally
{
colorPicker.ColorIsUpdating = false;
}
}
}
else
{
throw new InvalidCastException("Cannot convert the given input to a valid color");
}
}
}
/// <summary>
/// Gets or sets the name of the <see cref="SelectedColor"/>.
/// </summary>
public string? ColorName
{
get => (string?)this.GetValue(ColorNameProperty);
set => this.SetValue(ColorNameProperty, value);
}
/// <summary>Identifies the <see cref="ColorNamesDictionary"/> dependency property.</summary>
public static readonly DependencyProperty ColorNamesDictionaryProperty
= DependencyProperty.Register(nameof(ColorNamesDictionary),
typeof(Dictionary<Color, string>),
typeof(ColorPickerBase),View on GitHub (pinned to 72099e310b)
Solutions
- Validate the input string before assigning to ColorName — ensure it parses to a Color via the same ColorHelper/ColorNamesDictionary first.
- Provide a ColorNamesDictionary that includes the localized/custom names you accept, or restrict the input to known names.
- Set IsAlphaChannelVisible to match the hex format you feed in (e.g. #AARRGGBB when alpha is visible).
- Supply a custom ColorHelper that can parse your color name format instead of returning null.
Example fix
// before: unknown name throws inside the DP callback
ColorNameTextBox.TextChanged += (s,e) => vm.ColorName = ColorNameTextBox.Text;
// after: validate first
var helper = ColorHelper.DefaultInstance;
var parsed = helper.ColorFromString(input, vm.ColorNamesDictionary);
if (parsed is not null) vm.ColorName = input;
else vm.ColorError = $"'{input}' is not a valid color"; Defensive patterns
Strategy: validation
Validate before calling
// Validate the string can be parsed by the same helper before assigning ColorName
var helper = colorPicker.ColorHelper ?? ColorHelper.DefaultInstance;
var color = helper.ColorFromString(input, colorPicker.ColorNamesDictionary);
if (color is null) {
// show user-facing error, do NOT assign to ColorName
return;
}
colorPicker.ColorName = input; Try / catch
try {
colorPicker.ColorName = input;
} catch (InvalidCastException) {
// input not parseable; keep previous color, surface a user message
} Prevention
- Pre-validate color strings with the same ColorHelper + ColorNamesDictionary before binding.
- Constrain user input to a known list of color names or a hex pattern matching IsAlphaChannelVisible.
- Provide a ColorNamesDictionary that includes localized names you accept.
- Keep hex format consistent with IsAlphaChannelVisible (#RRGGBB vs #AARRGGBB).
When it happens
Trigger: ColorName is set (via binding or directly) to a value that ColorHelper.ColorFromString cannot parse — neither a recognized name nor a valid hex string — so the helper returns null and the callback throws InvalidCastException during the dependency-property change notification.
Common situations: User types an unrecognized color name in a bound TextBox. Binding feeds a localized name not present in ColorNamesDictionary. A hex string with the wrong format/length (#RGB vs #ARGB) for the configured IsAlphaChannelVisible. A custom ColorHelper that returns null for valid inputs.
Related errors
- Type must be for an Enum.
- The EnumType must be specified.
- Uups, it seems like there is something wrong with the given
- Unable to find the dialog closing storyboard. Did you forget
- Could not locate any instances of contract {contract}.
AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13).
Data as JSON: /api/errors/3762b763fbd4c38e.
Report an issue: GitHub.