Skip to content

Commit 3d9a355

Browse files
authored
Adding support in the PaletteHelper to update custom theme (#2369)
This is saves on adding duplicate resources to the root application.
1 parent 3e005e0 commit 3d9a355

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

MaterialDesignThemes.Wpf/BundledTheme.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace MaterialDesignThemes.Wpf
55
{
6-
public class BundledTheme : ResourceDictionary
6+
public class BundledTheme : ResourceDictionary, IMaterialDesignThemeDictionary
77
{
88
private BaseTheme? _baseTheme;
99
public BaseTheme? BaseTheme

MaterialDesignThemes.Wpf/CustomColorTheme.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace MaterialDesignThemes.Wpf
55
{
6-
public class CustomColorTheme : ResourceDictionary
6+
public class CustomColorTheme : ResourceDictionary, IMaterialDesignThemeDictionary
77
{
88
private BaseTheme? _baseTheme;
99
public BaseTheme? BaseTheme
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace MaterialDesignThemes.Wpf
2+
{
3+
public interface IMaterialDesignThemeDictionary
4+
{ }
5+
}
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Windows;
34

45
namespace MaterialDesignThemes.Wpf
@@ -8,23 +9,28 @@ public class PaletteHelper
89
public virtual ITheme GetTheme()
910
{
1011
if (Application.Current is null)
11-
throw new InvalidOperationException("Cannot get theme outside of a WPF application. Use ResourceDictionaryExtensions.GetTheme on the appropriate resource dictionary instead.");
12-
return Application.Current.Resources.GetTheme();
12+
throw new InvalidOperationException($"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.");
13+
return GetResourceDictionary().GetTheme();
1314
}
1415

1516
public virtual void SetTheme(ITheme theme)
1617
{
1718
if (theme is null) throw new ArgumentNullException(nameof(theme));
1819
if (Application.Current is null)
19-
throw new InvalidOperationException("Cannot set theme outside of a WPF application. Use ResourceDictionaryExtensions.SetTheme on the appropriate resource dictionary instead.");
20-
Application.Current.Resources.SetTheme(theme);
20+
throw new InvalidOperationException($"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead.");
21+
22+
GetResourceDictionary().SetTheme(theme);
2123
}
2224

2325
public virtual IThemeManager? GetThemeManager()
2426
{
2527
if (Application.Current is null)
26-
throw new InvalidOperationException("Cannot get ThemeManager. Use ResourceDictionaryExtensions.GetThemeManager on the appropriate resource dictionary instead.");
27-
return Application.Current.Resources.GetThemeManager();
28+
throw new InvalidOperationException($"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead.");
29+
return GetResourceDictionary().GetThemeManager();
2830
}
31+
32+
private static ResourceDictionary GetResourceDictionary()
33+
=> Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is IMaterialDesignThemeDictionary) ??
34+
Application.Current.Resources;
2935
}
3036
}

0 commit comments

Comments
 (0)