Skip to content

Commit 9164c17

Browse files
committed
parse resource dictionaries [skip ci]
1 parent 67a6121 commit 9164c17

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

MaterialDesignColors.WpfExample/MainWindow.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public MainWindow()
2828

2929
new CardsWindow().Show();
3030
new ListsWindow().Show();
31+
new PaletteSelectorWindow()
32+
{
33+
DataContext = new PaletteSelectorViewModel()
34+
}.Show();
3135
}
3236

3337
private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)

MaterialDesignColors.WpfExample/PaletteSelectorViewModel.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,41 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using System.Text.RegularExpressions;
7+
using System.Windows;
68

79
namespace MaterialDesignColors.WpfExample
810
{
911
public class PaletteSelectorViewModel
1012
{
11-
}
13+
public PaletteSelectorViewModel()
14+
{
15+
AscertainCurrentResourceDictionaries();
16+
}
17+
18+
private void AscertainCurrentResourceDictionaries()
19+
{
20+
var lightDarkResourceDictionary = Application.Current.Resources.MergedDictionaries
21+
.Where(rd => rd.Source != null)
22+
.SingleOrDefault(rd => Regex.Match(rd.Source.AbsolutePath, @"(\/MaterialDesignThemes.Wpf;component\/Themes\/MaterialDesignTheme\.)((Light)|(Dark))").Success);
23+
if (lightDarkResourceDictionary == null)
24+
throw new ApplicationException("Unable to find Light/Dark base theme in Application resources.");
25+
var lightDarkDescription = Regex.Match(lightDarkResourceDictionary.Source.AbsolutePath, "(?'desc'(Light)|(Dark))(.xaml)$").Groups[
26+
"desc"].Value;
27+
28+
ResourceDictionary primaryColorResourceDictionary;
29+
if (!TryFindSwatchDictionary(Application.Current.Resources, "PrimaryHueMidBrush", out primaryColorResourceDictionary))
30+
throw new ApplicationException("Unable to find primary color definition in Application resources.");
31+
32+
ResourceDictionary accentolorResourceDictionary;
33+
if (!TryFindSwatchDictionary(Application.Current.Resources, "SecondaryAccentBrush", out accentolorResourceDictionary))
34+
throw new ApplicationException("Unable to find accent color definition in Application resources.");
35+
}
36+
37+
private bool TryFindSwatchDictionary(ResourceDictionary parentDictionary, string expectedBrushName, out ResourceDictionary dictionary)
38+
{
39+
dictionary = parentDictionary.MergedDictionaries.SingleOrDefault(rd => rd[expectedBrushName] != null);
40+
return dictionary != null;
41+
}
42+
}
1243
}

0 commit comments

Comments
 (0)