docs/documentation/themes.md
WPF UI provides a robust theming system that allows you to control your application's appearance, including support for light, dark, and high contrast modes. The ApplicationThemeManager class is the primary tool for managing themes at runtime.
[!IMPORTANT] For theme changes to apply correctly, your colors and brushes should be referenced as
DynamicResource.
The easiest way to set the initial theme is by using the ThemesDictionary in your App.xaml. This ensures that the correct theme resources are loaded at startup.
<Application
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:ThemesDictionary Theme="Light" />
<ui:ControlsDictionary />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
The Theme property on ThemesDictionary can be set to Light or Dark.
Use the ApplicationThemeManager.Apply() method to change the theme while the application is running.
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
// Apply the Light theme with a Mica backdrop
ApplicationThemeManager.Apply(
ApplicationTheme.Light,
WindowBackdropType.Mica
);
ApplicationTheme EnumThis enum specifies the theme to apply:
Light: The standard light theme.Dark: The standard dark theme.HighContrast: Automatically selects the appropriate Windows High Contrast theme.You can synchronize your application's theme with the current Windows theme settings.
To apply the current system theme once, use ApplySystemTheme().
// Apply the current Windows theme (light or dark)
ApplicationThemeManager.ApplySystemTheme();
For continuous synchronization, use the SystemThemeWatcher. It automatically updates your app's theme and accent color when the user changes their Windows settings. See the SystemThemeWatcher documentation for more details.
using Wpf.Ui.Appearance;
public partial class MainWindow : System.Windows.Window
{
public MainWindow()
{
// Watch for system theme changes
SystemThemeWatcher.Watch(this);
InitializeComponent();
}
}
You can get the current application and system themes at any time.
ApplicationThemeManager.GetAppTheme(): Returns the current ApplicationTheme (Light, Dark, or HighContrast).ApplicationThemeManager.GetSystemTheme(): Returns the current SystemTheme.ApplicationTheme currentAppTheme = ApplicationThemeManager.GetAppTheme();
SystemTheme currentSystemTheme = ApplicationThemeManager.GetSystemTheme();
if (currentAppTheme == ApplicationTheme.Dark)
{
// ...
}
SystemTheme EnumThis enum represents the actual theme reported by Windows, including decorative themes like Glow, CapturedMotion, and Sunrise. ApplicationThemeManager maps these to either Light or Dark.
WPF UI automatically handles Windows High Contrast themes. When a high contrast mode is detected, ApplicationThemeManager loads the appropriate high contrast resource dictionary (HC1, HC2, HCBlack, or HCWhite).
ApplicationThemeManager.IsHighContrast(): Checks if the application is currently in a high contrast theme.ApplicationThemeManager.IsSystemHighContrast(): Checks if Windows is currently in a high contrast mode.The ApplicationThemeManager.Changed event is triggered whenever the application's theme is successfully changed.
ApplicationThemeManager.Changed += (currentTheme, currentAccent) =>
{
Debug.WriteLine($"Theme changed to {currentTheme} with accent {currentAccent}");
};
This event is useful for applying custom logic after a theme change, such as updating graphics or non-WPF UI elements.
[!TIP] The
Changedevent is fired by both manualApply()calls and automatic updates fromSystemThemeWatcher, providing a single place to react to any theme change.