MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException

Could not locate required resource with key(s) '{string.Join

Error message

Could not locate required resource with key(s) '{string.Join(", ", keys)}'

What it means

When ResourceDictionaryExtensions.GetTheme cannot find a cached Theme under the CurrentThemeKey, it reconstructs one from the MaterialDesign brush resources. GetColor scans the supplied keys for a SolidColorBrush; if none is found it throws InvalidOperationException listing the keys it tried. This means the target ResourceDictionary does not contain the expected MaterialDesign brushes.

Source

Thrown at src/MaterialDesignThemes.Wpf/ResourceDictionaryExtensions.cs:119

            SecondaryDark = new ColorPair(GetColor(resourceDictionary, "MaterialDesign.Brush.Secondary.Dark"),
                                          GetColor(resourceDictionary, "MaterialDesign.Brush.Secondary.Dark.Foreground")),
        };
        LoadThemeColors(resourceDictionary, theme);
        return theme;
    }

    private static partial void LoadThemeColors(ResourceDictionary resourceDictionary, Theme theme);

    private static Color GetColor(ResourceDictionary resourceDictionary, params string[] keys)
    {
        foreach (string key in keys)
        {
            if (TryGetColor(resourceDictionary, key, out Color color))
            {
                return color;
            }
        }
        throw new InvalidOperationException($"Could not locate required resource with key(s) '{string.Join(", ", keys)}'");
    }

    private static bool TryGetColor(ResourceDictionary resourceDictionary, string key, out Color color)
    {
        if (resourceDictionary[key] is SolidColorBrush brush)
        {
            color = brush.Color;
            return true;
        }
        color = default;
        return false;
    }

    public static IThemeManager? GetThemeManager(this ResourceDictionary resourceDictionary)
    {
        if (resourceDictionary is null) throw new ArgumentNullException(nameof(resourceDictionary));
        return resourceDictionary[ThemeManagerKey] as IThemeManager;
    }

View on GitHub (pinned to 98edec3a0b)

Solutions

  1. Merge the MaterialDesign resource dictionary (BundledTheme / MaterialDesignTheme) into the target dictionary before calling GetTheme.
  2. Call SetTheme(...) first so the CurrentThemeKey is populated and GetTheme returns it directly without needing the brushes.
  3. Ensure MaterialDesignThemes and MaterialDesignColors versions are aligned.

Example fix

<!-- before: empty dictionary -->
<ResourceDictionary x:Name="rd"/>
var t = rd.GetTheme(); // throws
<!-- after -->
rd.MergedDictionaries.Add(new BundledTheme());
rd.SetTheme(Theme.Create(...));
var t = rd.GetTheme();
Defensive patterns

Strategy: validation

Validate before calling

if (!rd.Contains("MaterialDesign.Brush.Primary"))
    rd.MergedDictionaries.Add(new BundledTheme());
rd.SetTheme(theme); // populates CurrentThemeKey so GetTheme won't need the brushes
var t = rd.GetTheme();

Type guard

resourceDictionary.Contains("MaterialDesign.Brush.Primary")

Try / catch

try { return rd.GetTheme(); }
catch (InvalidOperationException) { /* theme not yet applied */ rd.SetTheme(Theme.Create(...)); return rd.GetTheme(); }

Prevention

When it happens

Trigger: Calling resourceDictionary.GetTheme() on a dictionary that has not merged a MaterialDesign theme resource dictionary, or where the brush keys were removed/renamed (version mismatch between MaterialDesignThemes and MaterialDesignColors).

Common situations: Forgetting to add <materialDesign:BundledTheme/> or MaterialDesignTheme.xaml in App.xaml; calling GetTheme on a fresh empty ResourceDictionary; upgrading MaterialDesign and a brush key was renamed.

Related errors


AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13). Data as JSON: /api/errors/d3dae1708a9401f5. Report an issue: GitHub.