{"record":{"id":"1c78e173c52b392e","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"colorreference-does-not-contain-any-color","errorCode":null,"errorMessage":"ColorReference does not contain any color","messagePattern":"ColorReference does not contain any color","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignThemes.Wpf/ColorReference.cs","lineNumber":17,"sourceCode":"﻿using System.Windows.Media;\n\nnamespace MaterialDesignThemes.Wpf;\n\npublic readonly record struct ColorReference(ThemeColorReference ThemeReference, Color? Color)\n{\n    public static ColorReference SecondaryLight { get; } = new ColorReference(ThemeColorReference.SecondaryLight, null);\n    public static ColorReference SecondaryMid { get; } = new ColorReference(ThemeColorReference.SecondaryMid, null);\n    public static ColorReference SecondaryDark { get; } = new ColorReference(ThemeColorReference.SecondaryDark, null);\n    public static ColorReference PrimaryLight { get; } = new ColorReference(ThemeColorReference.PrimaryLight, null);\n    public static ColorReference PrimaryMid { get; } = new ColorReference(ThemeColorReference.PrimaryMid, null);\n    public static ColorReference PrimaryDark { get; } = new ColorReference(ThemeColorReference.PrimaryDark, null);\n\n    public static implicit operator ColorReference(Color color) => new(ThemeColorReference.None, color);\n    public static implicit operator ColorReference(ThemeColorReference @ref) => new(@ref, null);\n    public static implicit operator Color(ColorReference color) => color.Color ??\n        throw new InvalidOperationException($\"{nameof(ColorReference)} does not contain any color\");\n}\n","sourceCodeStart":1,"sourceCodeEnd":19,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignThemes.Wpf/ColorReference.cs#L1-L19","documentation":"`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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nColor c = ColorReference.SecondaryMid; // throws: no concrete color yet\n\n// after\nColor c = ColorReference.SecondaryMid.Color\n    ?? theme.SecondaryMid; // resolved from the applied palette","handlingStrategy":"type-guard","validationCode":"if (reference.Color is Color resolved)\n    target.ColorProperty = resolved;\nelse\n    target.ColorProperty = fallbackColor; // resolve via theme instead","typeGuard":"static bool IsResolved(ColorReference r) => r.Color is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["wpf","theming","color","implicit-conversion","nullable","material-design"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}