diff --git a/MaterialDesignThemes.Wpf/BundledTheme.cs b/MaterialDesignThemes.Wpf/BundledTheme.cs index c102173ce7..7c9400516c 100644 --- a/MaterialDesignThemes.Wpf/BundledTheme.cs +++ b/MaterialDesignThemes.Wpf/BundledTheme.cs @@ -3,7 +3,7 @@ namespace MaterialDesignThemes.Wpf { - public class BundledTheme : ResourceDictionary + public class BundledTheme : ResourceDictionary, IMaterialDesignThemeDictionary { private BaseTheme? _baseTheme; public BaseTheme? BaseTheme diff --git a/MaterialDesignThemes.Wpf/CustomColorTheme.cs b/MaterialDesignThemes.Wpf/CustomColorTheme.cs index 3dd1f86d4c..11cf8a1a0d 100644 --- a/MaterialDesignThemes.Wpf/CustomColorTheme.cs +++ b/MaterialDesignThemes.Wpf/CustomColorTheme.cs @@ -3,7 +3,7 @@ namespace MaterialDesignThemes.Wpf { - public class CustomColorTheme : ResourceDictionary + public class CustomColorTheme : ResourceDictionary, IMaterialDesignThemeDictionary { private BaseTheme? _baseTheme; public BaseTheme? BaseTheme diff --git a/MaterialDesignThemes.Wpf/IMaterialDesignThemeDictionary.cs b/MaterialDesignThemes.Wpf/IMaterialDesignThemeDictionary.cs new file mode 100644 index 0000000000..3fa7e12d12 --- /dev/null +++ b/MaterialDesignThemes.Wpf/IMaterialDesignThemeDictionary.cs @@ -0,0 +1,5 @@ +namespace MaterialDesignThemes.Wpf +{ + public interface IMaterialDesignThemeDictionary + { } +} \ No newline at end of file diff --git a/MaterialDesignThemes.Wpf/PaletteHelper.cs b/MaterialDesignThemes.Wpf/PaletteHelper.cs index 7706eadf32..ba99b6406d 100644 --- a/MaterialDesignThemes.Wpf/PaletteHelper.cs +++ b/MaterialDesignThemes.Wpf/PaletteHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Windows; namespace MaterialDesignThemes.Wpf @@ -8,23 +9,28 @@ public class PaletteHelper public virtual ITheme GetTheme() { if (Application.Current is null) - throw new InvalidOperationException("Cannot get theme outside of a WPF application. Use ResourceDictionaryExtensions.GetTheme on the appropriate resource dictionary instead."); - return Application.Current.Resources.GetTheme(); + throw new InvalidOperationException($"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead."); + return GetResourceDictionary().GetTheme(); } public virtual void SetTheme(ITheme theme) { if (theme is null) throw new ArgumentNullException(nameof(theme)); if (Application.Current is null) - throw new InvalidOperationException("Cannot set theme outside of a WPF application. Use ResourceDictionaryExtensions.SetTheme on the appropriate resource dictionary instead."); - Application.Current.Resources.SetTheme(theme); + throw new InvalidOperationException($"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead."); + + GetResourceDictionary().SetTheme(theme); } public virtual IThemeManager? GetThemeManager() { if (Application.Current is null) - throw new InvalidOperationException("Cannot get ThemeManager. Use ResourceDictionaryExtensions.GetThemeManager on the appropriate resource dictionary instead."); - return Application.Current.Resources.GetThemeManager(); + throw new InvalidOperationException($"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead."); + return GetResourceDictionary().GetThemeManager(); } + + private static ResourceDictionary GetResourceDictionary() + => Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is IMaterialDesignThemeDictionary) ?? + Application.Current.Resources; } }