MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException
ColorReference does not contain any color
Error message
ColorReference does not contain any color
What it means
`ColorReference` is a readonly record struct pairing a `ThemeColorReference` (e.g. `PrimaryMid`) with an optional `Color`. The implicit conversion to `Color` throws `InvalidOperationException` when only the theme reference is set and no concrete `Color` has been resolved (e.g. `ColorReference.SecondaryMid` before a theme is applied).
Source
Thrown at src/MaterialDesignThemes.Wpf/ColorReference.cs:17
using System.Windows.Media;
namespace MaterialDesignThemes.Wpf;
public readonly record struct ColorReference(ThemeColorReference ThemeReference, Color? Color)
{
public static ColorReference SecondaryLight { get; } = new ColorReference(ThemeColorReference.SecondaryLight, null);
public static ColorReference SecondaryMid { get; } = new ColorReference(ThemeColorReference.SecondaryMid, null);
public static ColorReference SecondaryDark { get; } = new ColorReference(ThemeColorReference.SecondaryDark, null);
public static ColorReference PrimaryLight { get; } = new ColorReference(ThemeColorReference.PrimaryLight, null);
public static ColorReference PrimaryMid { get; } = new ColorReference(ThemeColorReference.PrimaryMid, null);
public static ColorReference PrimaryDark { get; } = new ColorReference(ThemeColorReference.PrimaryDark, null);
public static implicit operator ColorReference(Color color) => new(ThemeColorReference.None, color);
public static implicit operator ColorReference(ThemeColorReference @ref) => new(@ref, null);
public static implicit operator Color(ColorReference color) => color.Color ??
throw new InvalidOperationException($"{nameof(ColorReference)} does not contain any color");
}
View on GitHub (pinned to 98edec3a0b)
Solutions
- Check the nullable `Color` property before converting: `if (ref.Color is Color c) use(c);`.
- Resolve the reference through the current theme/palette to obtain a concrete `Color` before conversion.
- Provide a fallback colour when `Color` is null instead of relying on the implicit cast.
- Avoid using the static theme-only defaults where a `Color` is directly required.
Example fix
// before
Color c = ColorReference.SecondaryMid; // throws: no concrete color yet
// after
Color c = ColorReference.SecondaryMid.Color
?? theme.SecondaryMid; // resolved from the applied palette Defensive patterns
Strategy: type-guard
Validate before calling
if (reference.Color is Color resolved)
target.ColorProperty = resolved;
else
target.ColorProperty = fallbackColor; // resolve via theme instead Type guard
static bool IsResolved(ColorReference r) => r.Color is not null;
Prevention
- Always read the nullable .Color property instead of relying on the implicit cast.
- Resolve ColorReference through the applied theme/palette before converting.
- Keep a fallback colour when the reference is theme-only.
When it happens
Trigger: Implicitly or explicitly converting a theme-only `ColorReference` (one of the static defaults like `PrimaryMid`, `SecondaryLight`, etc., all of which hold `Color: null`) to `System.Windows.Media.Color` before the theme/badge has supplied an actual colour.
Common situations: Using `ColorReference.SecondaryMid` as a default and immediately assigning it to a `Color`-typed property; converting references before `PaletteHelper`/`Theme` has set the secondary palette; relying on the static defaults at design time.
Related errors
- Entry {entry.Key} was not of type Color
- Entry {foregroundKey} was not of type Color
- Non primary hues provided.
- Invalid HSB values
- kinds must contain at least one value
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/1c78e173c52b392e.
Report an issue: GitHub.