PowerShell and WPF: Dynamically change theme and accent of a GUI with MahApps 2

In this post I will show you how to change the main theme and accent from a PowerShell and WPF application with MahApps 2.0 directly from a button in the GUI.

Previous post
I did a prvious post about thare, here.
In the new version of MahApps, the process is a bit different.
Indeed we will now use the assembly ControlzEx.dll to manage theme and accent.
Thanks to my friend Jay aka JM2K69 for the hint about this new process.

How to get the sample
Click on the GitHub picture below to get the sample:

Theme and accent
Before to start, here is a quick reminder about Mahapps Theme and Accent.

The theme is the main design of the GUI. It will work like Windows 10 with two themes:
- Dark: Dark theme, meaning black
- Light: Light theme, meaning white

The Accent will be used to change all colors of you Controls in one click.

In MahApps, you can manage both easily by changing just one line from teh resource block. The third line of the below block:
In this example the value is Light.Cobalt meaning YourTheme.YourAccent
To change the theme to Dark, just replace Light with Dark and value will be Dark.Cobalt
To change accent to Red, just replace Cobalt with Red and value will be Dark.Red

Do this with PowerShell
With the new version of MahApps, managing theme and accent can be done from the assembly ControlzEx.
To manage themes and accent, you will need to use the ThemeManager from ControlzEx class.
This is used as below:

Different methods exist to manage themes, to detect and change the theme:
- DetectTheme: Detect the current theme and accent
- ChangeThemeBaseColor: Change the current theme 
- ChangeThemeColorScheme: Change the current accent

The ThemeManager class contains a Current method, allowing you to detect current informations. We will then add our previous methods to this one.

In terms of PowerShell this will translate as follows:

Managing the theme
Detect the current theme
As seen above, the method to detect the current theme used in your app is DetectTheme.
Below is the PowerShell code to use this method:
Here the variable $Form is the XAML interface charged from PowerShell.

The result obtained by the variable $Theme is as follows:

We can find our value from the resource block Theme.Accent, here Light.Cobalt.
It also informs us that the used theme an accent is Cobalt (Light).
There are two important properties for us BaseColorScheme and ColorScheme which will allow respectively to make below actions:
- BaseColorScheme: Change the theme
- ColorScheme: Change the accent

Change the current theme
The theme change is done through two methods:
- DetectTheme
- ChangeThemeBaseColor

The first will have the current theme while the second will be used to change it.
To change value, just add Light and Dark.

In the below example we will change the current theme to Dark
Now to Light
Now let's get further and detect the current theme.
If this one is Light, then we will change it to Dark and if it Dark we will change it to Light.

Managing Accent
Detect current accent
The idea is the same than for the theme.
To detect the current accent (colors) we will use the same class and methods:
As mentioned previously, the Accent value is stored is a property ColorScheme.

Here the current accent is Cobalt.
You can store this information in a variable, as below:
Change accent (colors)
The accent change is done through two methods:
- DetectTheme
- ChangeThemeColorScheme

The first will have the current accent while the second will be used to change it.
To change value, just add the accent value like Cobalt, Red, Pink…
In the below example we will change the current accent to Red
Now to Cobalt
Or Pink

In the below application, combobox allows me to change dynamically theme and accent from the GUI.

WPF_Mahapps 3886462518443927552

Enregistrer un commentaire

Accueil item



Learn KQL in one month

You want to support me ?

Mes articles en français

Books in French