Chapter.Net.WPF.Theming 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chapter.Net.WPF.Theming --version 1.1.0                
NuGet\Install-Package Chapter.Net.WPF.Theming -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Chapter.Net.WPF.Theming" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chapter.Net.WPF.Theming --version 1.1.0                
#r "nuget: Chapter.Net.WPF.Theming, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Chapter.Net.WPF.Theming as a Cake Addin
#addin nuget:?package=Chapter.Net.WPF.Theming&version=1.1.0

// Install Chapter.Net.WPF.Theming as a Cake Tool
#tool nuget:?package=Chapter.Net.WPF.Theming&version=1.1.0                

<img src="https://raw.githubusercontent.com/dwndland/Chapter.Net.WPF.Theming/master/Icon.png" alt="logo" width="64"/>

Chapter.Net.WPF.Theming Library

Overview

Brings features to work with the windows themes and accent colors under WPF.

Features (Single Operations)

  • SystemThemeProvider: Read the current theme from windows. Light or Dark.
  • AccentColorProvider: Read all the current accent colors from windows.
  • AccentBrush: Set a particular grade from the current accent color as a brush to a control in xaml.
  • AccentColor: Set a particular grade from the current accent to a color to a brush in xaml.
  • ThemeManager.RequestTheme: Use xaml attached property to set a specific theme on a window.
  • ThemeManager.SetWindowTheme: Use code to set a specific theme on a window.
  • ThemedWindow: Use the ThemedWindow to create a window that has a build in functionality to set a particular theme.
  • ColorSetChangeObserver: Get informed when on windows the theme or accent color
  • Add/Remove Theme Resources: Use the ResourcesManager to add and remove theme resources.

Features (Complete Operations)

  • Complete Theming: Configure the theme globally for all windows by attached properties or the ThemedWindow with possibility for auto theme by windows or manual override.

Getting Started

  1. Installation:

    • Install the Chapter.Net.WPF.Theming library via NuGet Package Manager:
    dotnet add package Chapter.Net.WPF.Theming
    
  2. SystemThemeProvider:

    • Read the current theme from windows. Light or Dark.
    var theme = SystemThemeProvider.GetSystemTheme();
    
  3. AccentColorProvider:

    • Read all the current accent colors from windows.
    var accent = AccentColorProvider.GetAccentColor(Accent.SystemAccent);
    var accentLight2 = AccentColorProvider.GetAccentColor(Accent.SystemAccentLight2);
    
  4. AccentBrush:

    • Set a particular grade from the current accent color as a brush to a control in xaml.
    <TextBlock Background="{theming:AccentBrush SystemAccentLight2}"
               Foreground="{theming:AccentBrush SystemAccentLight2, UseForeground=True}"
               Text="SystemAccentLight2" />
    
  5. AccentColor:

    • Set a particular grade from the current accent to a color to a brush in xaml.
    <TextBlock Background="{StaticResource background}"
               Foreground="{StaticResource foreground}"
               Text="SystemAccentLight2">
        <TextBlock.Resources>
            <SolidColorBrush x:Key="background" Color="{theming:AccentColor SystemAccentLight2}" />
            <SolidColorBrush x:Key="foreground" Color="{theming:AccentColor SystemAccentLight2, UseForeground=True}" />
        </TextBlock.Resources>
    </TextBlock>
    
  6. ThemeManager.RequestTheme:

    • Use xaml attached property to set a specific theme on a window.
    <Window xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
            theming:ThemeManager.RequestTheme="System" />
    
    <Window xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
            theming:ThemeManager.RequestTheme="Light" />
    
    <Window xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
            theming:ThemeManager.RequestTheme="Dark" />
    
  7. ThemeManager.SetWindowTheme:

    • Use code to set a specific theme on a window.
    public partial class MyWindow
    {
        public MyWindow()
        {
            ThemeManager.SetWindowTheme(this, WindowTheme.System);
            InitializeComponent();
        }
    }
    
  8. ThemedWindow:

    • Use the ThemedWindow to create a window that has a build in functionality to set a particular theme.
    <theming:ThemedWindow xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
                          RequestTheme="System" />
    
    <theming:ThemedWindow xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
                          RequestTheme="Light" />
    
    <theming:ThemedWindow xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
                          RequestTheme="Dark" />
    
  9. ColorSetChangeObserver:

    • Get informed when on windows the theme or accent color
    ColorSetChangeObserver.AddCallback(OnSystemColorChanged);
    
    private void OnSystemColorChanged()
    {
        // Window theme or accent color changed.
    }
    
  10. Add/Remove Theme Resources:

    • Use the ResourcesManager to add and remove theme resources.
    private Uri _light = new("/DemoControls;component/Themes/Light.xaml", UriKind.RelativeOrAbsolute);
    private Uri _dark = new("/DemoControls;component/Themes/Dark.xaml", UriKind.RelativeOrAbsolute);
    
    private void SwitchToLightResources(object sender, RoutedEventArgs e)
    {
        ResourcesManager.RemoveResources(Application.Current, _dark);
        ResourcesManager.LoadResources(Application.Current, ResourceLocation.End, _light);
    }
    
    private void SwitchToDarkResources(object sender, RoutedEventArgs e)
    {
        ResourcesManager.RemoveResources(Application.Current, _light);
        ResourcesManager.LoadResources(Application.Current, ResourceLocation.End, _dark);
    }
    
  11. Complete Theming:

    • Configure the theme globally for all windows by attached properties or the ThemedWindow with possibility for auto theme by windows or manual override.

    Register theme resources to use when switch the theme.

    public partial class App
    {
        public App()
        {
            ResourcesManager.RegisterResources(this, ResourceLocation.End, WindowTheme.Light, new Uri("/DemoControls;component/Themes/Light.xaml", UriKind.RelativeOrAbsolute));
            ResourcesManager.RegisterResources(this, ResourceLocation.End, WindowTheme.Dark, new Uri("/DemoControls;component/Themes/Dark.xaml", UriKind.RelativeOrAbsolute));
        }
    }
    

    Configure all the windows to obey the theme manager.

    <Window xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
            theming:ThemeManager.ObeyThemeManager="True" />
    
    <theming:ThemedWindow xmlns:theming="clr-namespace:Chapter.Net.WPF.Theming;assembly=Chapter.Net.WPF.Theming"
                          ObeyThemeManager="True" />
    

    Init the theme manager

    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
    
            // Support for update of all windows when they are set to system.
            ColorSetChangeObserver.StartListenForColorChanges(this);
    
            // All windows obey the ThemeManager, so this is the init for all windows.
            ThemeManager.SetCurrentTheme(WindowTheme.System);
        }
    }
    

    Set new theme at runtime

    private static void OnNewThemeSaved(WindowTheme theme)
    {
        ThemeManager.SetCurrentTheme(theme);
    }
    

Notes

  • The accent colors (AccentBrush, AccentColor) use a color cache internally for best performance. To reset the cache call AccentBrush.ResetColorCache or AccentColor.ResetColorCache.
  • The color cache gets reset automatically when the system color changes are observed using the ColorSetChangeObserver with the main window.
  • If any theme gets applied to a window, it sets also the window body color. To avoid that set ThemeManager.SkipSetBodyColors to true.
  • Any request of a theme is on System, it updated automatically if the theme got changed on windows at runtime.

License

Copyright (c) David Wendland. All rights reserved. Licensed under the MIT License. See LICENSE file in the project root for full license information.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net5.0-windows7.0 is compatible.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net6.0-windows7.0 is compatible.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible. 
.NET Core netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Chapter.Net.WPF.Theming:

Package Downloads
Chapter.Net.WPF.Behaviors

Brings additional behaviors to existing WPF controls.

Chapter.Net.WPF.MessageBoxes

An message box made in WPF with more features and possibility to style.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 89 6/7/2024
1.1.1 130 4/28/2024
1.1.0 123 4/27/2024
1.0.0 143 4/14/2024