Skip to content

Commit 569ff42

Browse files
authored
Created a new ThemeAssist class that allows for easy overriding of the color themes. (#1036)
Added a new property to the DialogHost for setting the theme color of the dialog. Defaulted the normal dialog host to light to maintain backward compatibility.
1 parent 70530f3 commit 569ff42

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

MaterialDesignThemes.Wpf/BaseTheme.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MaterialDesignThemes.Wpf
2+
{
3+
public enum BaseTheme
4+
{
5+
Inherit,
6+
Light,
7+
Dark
8+
}
9+
}

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,18 @@ public SnackbarMessageQueue SnackbarMessageQueue
414414
set { SetValue(SnackbarMessageQueueProperty, value); }
415415
}
416416

417+
public static readonly DependencyProperty DialogThemeProperty =
418+
DependencyProperty.Register(nameof(DialogTheme), typeof(BaseTheme), typeof(DialogHost), new PropertyMetadata(default(BaseTheme)));
419+
420+
/// <summary>
421+
/// Set the theme (light/dark) for the dialog.
422+
/// </summary>
423+
public BaseTheme DialogTheme
424+
{
425+
get { return (BaseTheme)GetValue(DialogThemeProperty); }
426+
set { SetValue(DialogThemeProperty, value); }
427+
}
428+
417429
public static readonly DependencyProperty PopupStyleProperty = DependencyProperty.Register(
418430
nameof(PopupStyle), typeof(Style), typeof(DialogHost), new PropertyMetadata(default(Style)));
419431

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
</ItemGroup>
262262
<ItemGroup>
263263
<Compile Include="Badged.cs" />
264+
<Compile Include="BaseTheme.cs" />
264265
<Compile Include="ButtonProgressAssist.cs" />
265266
<Compile Include="Card.cs" />
266267
<Compile Include="Chip.cs" />
@@ -341,6 +342,7 @@
341342
<Compile Include="SnackbarMessageQueue.cs" />
342343
<Compile Include="SnackbarMessageQueueItem.cs" />
343344
<Compile Include="Spelling.cs" />
345+
<Compile Include="ThemeAssist.cs" />
344346
<Compile Include="Transitions\CircleWipe.cs" />
345347
<Compile Include="IHintProxy.cs" />
346348
<Compile Include="Transitions\FadeWipe.cs" />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Windows;
3+
4+
namespace MaterialDesignThemes.Wpf
5+
{
6+
public static class ThemeAssist
7+
{
8+
public static BaseTheme GetTheme(DependencyObject obj)
9+
{
10+
return (BaseTheme)obj.GetValue(ThemeProperty);
11+
}
12+
13+
public static void SetTheme(DependencyObject obj, BaseTheme value)
14+
{
15+
obj.SetValue(ThemeProperty, value);
16+
}
17+
18+
public static readonly DependencyProperty ThemeProperty =
19+
DependencyProperty.RegisterAttached("Theme", typeof(BaseTheme), typeof(ThemeAssist), new PropertyMetadata(default(BaseTheme), OnThemeChanged));
20+
21+
private static void OnThemeChanged(DependencyObject @do, DependencyPropertyChangedEventArgs e)
22+
{
23+
if (@do is FrameworkElement element)
24+
{
25+
if (e.OldValue is BaseTheme oldTheme &&
26+
GetResourceDictionarySource(oldTheme) is string oldSource)
27+
{
28+
foreach(ResourceDictionary resourceDictionary in element.Resources.MergedDictionaries)
29+
{
30+
if (string.Equals(resourceDictionary.Source.ToString(), oldSource, StringComparison.Ordinal))
31+
{
32+
element.Resources.MergedDictionaries.Remove(resourceDictionary);
33+
break;
34+
}
35+
}
36+
}
37+
38+
if (e.NewValue is BaseTheme newTheme &&
39+
GetResourceDictionarySource(newTheme) is string newThemeSource)
40+
{
41+
element.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(newThemeSource) });
42+
}
43+
}
44+
}
45+
46+
private static string GetResourceDictionarySource(BaseTheme theme)
47+
{
48+
switch (theme)
49+
{
50+
case BaseTheme.Light:
51+
return "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml";
52+
case BaseTheme.Dark:
53+
return "pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml";
54+
}
55+
return null;
56+
}
57+
}
58+
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.DialogHost.xaml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Style TargetType="{x:Type wpf:DialogHost}">
1717
<Setter Property="DialogMargin" Value="35" />
1818
<Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth5" />
19+
<Setter Property="DialogTheme" Value="Light" />
1920
<Setter Property="PopupStyle" Value="{StaticResource MaterialDesignDialogHostPopup}" />
2021
<Setter Property="Template">
2122
<Setter.Value>
@@ -135,15 +136,9 @@
135136
</VisualStateGroup>
136137
</VisualStateManager.VisualStateGroups>
137138
<controlzEx:PopupEx PlacementTarget="{Binding ElementName=DialogHostRoot, Mode=OneWay}"
138-
x:Name="PART_Popup"
139-
Style="{TemplateBinding PopupStyle}">
140-
<controlzEx:PopupEx.Resources>
141-
<ResourceDictionary>
142-
<ResourceDictionary.MergedDictionaries>
143-
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
144-
</ResourceDictionary.MergedDictionaries>
145-
</ResourceDictionary>
146-
</controlzEx:PopupEx.Resources>
139+
x:Name="PART_Popup"
140+
Style="{TemplateBinding PopupStyle}"
141+
wpf:ThemeAssist.Theme="{TemplateBinding DialogTheme}">
147142
<wpf:Card x:Name="PART_PopupContentElement"
148143
Margin="{TemplateBinding DialogMargin}"
149144
wpf:ShadowAssist.ShadowDepth="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth)}"
@@ -319,7 +314,7 @@
319314

320315
<Grid x:Name="PART_ContentCoverGrid" Background="{x:Null}" Opacity="1" IsHitTestVisible="False" Focusable="False" />
321316

322-
<Grid x:Name="PART_Popup"
317+
<Grid x:Name="PART_Popup" wpf:ThemeAssist.Theme="{TemplateBinding DialogTheme}"
323318
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
324319
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
325320
<wpf:Card

0 commit comments

Comments
 (0)