Skip to content

Commit

Permalink
Avoid injecting DefaultThemeResources multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Oct 21, 2019
1 parent 4cf7283 commit f2d0cbd
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions ModernWpf/ThemeResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,7 @@ private ResourceDictionary GetThemeDictionary(string key)

if (ThemeDictionaries.TryGetValue(key, out ResourceDictionary themeDictionary))
{
if (themeDictionary.MergedDictionaries.Count > 0 &&
themeDictionary.MergedDictionaries[0] is DefaultThemeResources)
{
// Already contains the default theme resources
}
else
if (!ContainsDefaultThemeResources(themeDictionary))
{
themeDictionary.MergedDictionaries.Insert(0, defaultThemeDictionary);
}
Expand All @@ -307,5 +302,24 @@ private ResourceDictionary GetThemeDictionary(string key)

return themeDictionary;
}

private static bool ContainsDefaultThemeResources(ResourceDictionary dictionary)
{
if (dictionary is DefaultThemeResources)
{
return true;
}

foreach (var mergedDictionary in dictionary.MergedDictionaries)
{
if (mergedDictionary is DefaultThemeResources ||
mergedDictionary != null && ContainsDefaultThemeResources(mergedDictionary))
{
return true;
}
}

return false;
}
}
}

0 comments on commit f2d0cbd

Please sign in to comment.