From f2d0cbda6c60d6ca8bc1b215713f1a191fe24659 Mon Sep 17 00:00:00 2001 From: Yimeng Wu Date: Tue, 22 Oct 2019 04:06:56 +0800 Subject: [PATCH] Avoid injecting DefaultThemeResources multiple times --- ModernWpf/ThemeResources.cs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/ModernWpf/ThemeResources.cs b/ModernWpf/ThemeResources.cs index 93fd0f76..b1d45e4e 100644 --- a/ModernWpf/ThemeResources.cs +++ b/ModernWpf/ThemeResources.cs @@ -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); } @@ -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; + } } }