Skip to content

Instantly share code, notes, and snippets.

@McTopaz
Last active May 27, 2021 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McTopaz/d66ece110b396cb878a784edd63d73a9 to your computer and use it in GitHub Desktop.
Save McTopaz/d66ece110b396cb878a784edd63d73a9 to your computer and use it in GitHub Desktop.
Custom backgrounds for light and dark themes
<Application x:Class="MaterialDesignTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MaterialDesignTest"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
namespace MaterialDesignTest
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
PaletteHelper PaletteHelper { get; } = new PaletteHelper();
MaterialDesignLightTheme LightTheme { get; } = new MaterialDesignLightTheme();
MaterialDesignDarkTheme DarkTheme { get; } = new MaterialDesignDarkTheme();
public BaseTheme? CurrentTheme { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var bundle = Resources
.MergedDictionaries
.Where(d => d is BundledTheme)
.Cast<BundledTheme>()
.First();
CurrentTheme = bundle.BaseTheme;
}
public void SwitchTheme()
{
var isLight = CurrentTheme == BaseTheme.Light;
var theme = PaletteHelper.GetTheme();
var baseTheme = isLight ? Theme.Dark : Theme.Light;
if (isLight)
{
theme = new CustomDarkTheme();
}
else
{
theme = new CustomLightTheme();
}
theme.SetBaseTheme(baseTheme);
PaletteHelper.SetTheme(theme);
CurrentTheme = isLight ? BaseTheme.Dark : BaseTheme.Light;
}
}
public class CustomDarkTheme : ITheme
{
public ColorPair PrimaryLight { get; set; } = Colors.Red;
public ColorPair PrimaryMid { get; set; } = Colors.Lime;
public ColorPair PrimaryDark { get; set; } = Colors.Blue;
public ColorPair SecondaryLight { get; set; } = Colors.Orchid;
public ColorPair SecondaryMid { get; set; } = Colors.Turquoise;
public ColorPair SecondaryDark { get; set; } = Colors.Olive;
public Color ValidationError { get; set; } = Colors.Orange;
public Color Background { get; set; } = Colors.Salmon; // Won't work?
public Color Paper { get; set; } = Colors.SeaGreen; // Won't work?
public Color CardBackground { get; set; } = Colors.SlateGray; // Won't work?
public Color ToolBarBackground { get; set; }
public Color Body { get; set; }
public Color BodyLight { get; set; }
public Color ColumnHeader { get; set; }
public Color CheckBoxOff { get; set; }
public Color CheckBoxDisabled { get; set; }
public Color Divider { get; set; }
public Color Selection { get; set; }
public Color ToolForeground { get; set; }
public Color ToolBackground { get; set; }
public Color FlatButtonClick { get; set; }
public Color FlatButtonRipple { get; set; }
public Color ToolTipBackground { get; set; }
public Color ChipBackground { get; set; }
public Color SnackbarBackground { get; set; }
public Color SnackbarMouseOver { get; set; }
public Color SnackbarRipple { get; set; }
public Color TextBoxBorder { get; set; }
public Color TextFieldBoxBackground { get; set; }
public Color TextFieldBoxHoverBackground { get; set; }
public Color TextFieldBoxDisabledBackground { get; set; }
public Color TextAreaBorder { get; set; }
public Color TextAreaInactiveBorder { get; set; }
public Color DataGridRowHoverBackground { get; set; }
}
public class CustomLightTheme : ITheme
{
public ColorPair PrimaryLight { get; set; } = Colors.DarkRed;
public ColorPair PrimaryMid { get; set; } = Colors.DarkGreen;
public ColorPair PrimaryDark { get; set; } = Colors.DarkBlue;
public ColorPair SecondaryLight { get; set; } = Colors.DarkOrchid;
public ColorPair SecondaryMid { get; set; } = Colors.DarkTurquoise;
public ColorPair SecondaryDark { get; set; } = Colors.DarkOliveGreen;
public Color ValidationError { get; set; } = Colors.Orange;
public Color Background { get; set; } = Colors.DarkSalmon; // Won't work?
public Color Paper { get; set; } = Colors.DarkSeaGreen; // Won't work?
public Color CardBackground { get; set; } = Colors.DarkSlateGray; // Won't work?
public Color ToolBarBackground { get; set; }
public Color Body { get; set; }
public Color BodyLight { get; set; }
public Color ColumnHeader { get; set; }
public Color CheckBoxOff { get; set; }
public Color CheckBoxDisabled { get; set; }
public Color Divider { get; set; }
public Color Selection { get; set; }
public Color ToolForeground { get; set; }
public Color ToolBackground { get; set; }
public Color FlatButtonClick { get; set; }
public Color FlatButtonRipple { get; set; }
public Color ToolTipBackground { get; set; }
public Color ChipBackground { get; set; }
public Color SnackbarBackground { get; set; }
public Color SnackbarMouseOver { get; set; }
public Color SnackbarRipple { get; set; }
public Color TextBoxBorder { get; set; }
public Color TextFieldBoxBackground { get; set; }
public Color TextFieldBoxHoverBackground { get; set; }
public Color TextFieldBoxDisabledBackground { get; set; }
public Color TextAreaBorder { get; set; }
public Color TextAreaInactiveBorder { get; set; }
public Color DataGridRowHoverBackground { get; set; }
}
}
<Window x:Class="MaterialDesignTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MaterialDesignTest"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
Background="{DynamicResource MaterialDesignPaper}"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
Title="MainWindow" Height="Auto" Width="500">
<!--
https://intellitect.com/getting-started-material-design-in-xaml/
https://programmer.ink/think/wpf-uses-material-design-to-make-a-beautiful-login-page.html
https://gitter.im/ButchersBoy/MaterialDesignInXamlToolkit
-->
<StackPanel Orientation="Vertical">
<Separator Height="12" Visibility="Hidden" />
<!-- Change theme and some button -->
<DockPanel>
<StackPanel Orientation="Horizontal"
Margin="12,0,0,0"
DockPanel.Dock="Left"
HorizontalAlignment="Left">
<materialDesign:PackIcon Kind="WhiteBalanceSunny"
Margin="0,0,6,0"
VerticalAlignment="Center"/>
<ToggleButton Style="{StaticResource MaterialDesignSwitchToggleButton}"
Click="ToggleButton_Click"/>
<materialDesign:PackIcon Kind="MoonWaxingCrescent"
Margin="6,0,0,0"
VerticalAlignment="Center" />
</StackPanel>
<Button Style="{DynamicResource MaterialDesignRaisedAccentButton}"
DockPanel.Dock="Right"
Padding="20,0"
Margin="0,0,12,0"
Click="Button_Click"
Content="Click"/>
<Separator Visibility="Hidden" />
</DockPanel>
<Separator Height="12" Visibility="Hidden" />
<materialDesign:Card HorizontalAlignment="Center"
VerticalAlignment="Center"
Padding="10" >
<StackPanel>
<TextBlock Text="1337 asdf wasd DEADBEEF" />
</StackPanel>
</materialDesign:Card>
<Separator Height="12" Visibility="Hidden" />
<TextBlock Background="{DynamicResource PrimaryHueLightBrush}"
Foreground="{DynamicResource PrimaryHueLightForegroundBrush}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="PrimaryHueLightBrush / PrimaryHueLightForegroundBrush" />
<TextBlock Background="{DynamicResource PrimaryHueMidBrush}"
Foreground="{DynamicResource PrimaryHueMidForegroundBrush}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="PrimaryHueMidBrush / PrimaryHueMidForegroundBrush" />
<TextBlock Background="{DynamicResource PrimaryHueDarkBrush}"
Foreground="{DynamicResource PrimaryHueDarkForegroundBrush}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="PrimaryHueDarkBrush / PrimaryHueDarkForegroundBrush" />
<TextBlock Background="{DynamicResource SecondaryHueLightBrush}"
Foreground="{DynamicResource SecondaryHueLightForegroundBrush}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="SecondaryHueLightBrush / SecondaryHueLightForegroundBrush" />
<TextBlock Background="{DynamicResource SecondaryHueMidBrush}"
Foreground="{DynamicResource SecondaryHueMidForegroundBrush}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="SecondaryHueMidBrush / SecondaryHueMidForegroundBrush" />
<TextBlock Background="{DynamicResource SecondaryHueDarkBrush}"
Foreground="{DynamicResource SecondaryHueDarkForegroundBrush}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="SecondaryHueDarkBrush / SecondaryHueDarkBrush" />
<TextBlock Background="{DynamicResource MaterialDesignBackground}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="MaterialDesignBackground" />
<TextBlock Background="{DynamicResource MaterialDesignPaper}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="MaterialDesignPaper" />
<TextBlock Background="{DynamicResource MaterialDesignCardBackground}"
Width="400"
Margin="12"
TextAlignment="Center"
Text="MaterialDesignCardBackground" />
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MaterialDesignThemes.Wpf;
namespace MaterialDesignTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Nothing");
}
private void ToggleButton_Click(object sender, RoutedEventArgs e)
{
var app = Application.Current as App;
app.SwitchTheme();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment