Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Powershell WPF Themes guide step by step

Part 1: Basic Powershell WPF Themes guide step by step
Part 2: Powershell WPF Themes guide step by step
Part 3: Powershell wpf MahApps.Metro theme step by step

I have wrote many powershell GUI scripts but never thought using any themes on WPF GUI forms, As it is not required for internal organization use and it can be used without any different skin, but as I started sharing my GUI scripts on github.com, adding GUI to it makes more professional and attractive. I am here adding basic themes which I am downloading for one time using Visual Studio. Open Tools menu and expand NuGet Package Manager then select Package Manager Console.

On the Package Manager Console type command Install-Package wpf.themes. This start downloading themes packages from nuGet gallary https://www.nuget.org/packages/Wpf.Themes/. Location of download is as shown in the screenshot it might be different for you and can be found in VS wpf project folder Wpf.Themes.1.1.0\content\Themes. It has themes xaml files. I have copied this themes folder as it is portable and I copied it some other place so In my powershell script it can take the path from one central location.

Microsoft visual studio powershell wpf Install-package wpf.themes, easy ways to add packages to powershell wpf gui themes.png

I have all my ps1 files and themes folders are kept under C:\Temp folder location. Mentioned the theme xaml filename with folder path location as below on the 4th and 5th top line of the script. This creates the path name of the xaml file.

$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$ThemeFile = Join-Path -Path $ScriptPath -ChildPath Themes\BureauBlack.xaml

And (yellow highlighted code in the screenshot) copy code after Title and before grid control as it is. 

        <Window.Resources>
             <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="$ThemeFile" /> 
                </ResourceDictionary.MergedDictionaries>
             </ResourceDictionary>
         </Window.Resources>

Powershell WPF Window resources dictionary source, powershell gui script themes default schemes xaml, xmlns

After launching the script, theme shows good but with issue font color for checkbox and radiobutton are not showing correct and color is white as seen in the first screenshot.. 

Powershell wpf script theme test utility, all controls, windows platform frameworks, winforms, graphical user interface.png

To resolve this issue I am adding few more lines to XAML code and in one go changing font color for all CheckBoxes and RadioButtons to black, Here you can change all controls color with this small code, I have mentioned 2 style target types, it need to insert between closed MergeDictionaries and closed ResourceDictionary.

                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="Foreground" Value="Black"/>
                </Style>
                <Style TargetType="{x:Type RadioButton}">
                    <Setter Property="Foreground" Value="Black"/>
                </Style>

Microsoft window powershell wpf, windows resources style targettype xmlns xaml modification vistual studio code, code and theme formatting

Below are all the themes tested they are good and working fine. Adding form background color to the form makes it more good looking, Background="ColorName" property can be added to title object. Below screenshot is from Gregcons.com.

gregcons.com powershell wpf themes defaults.png

Complete code can be found here, It is also available on Github.com. In next articles I will be covering some more common themes to be used.

Useful Articles
Part 1: Create WPF XAML powershell GUI form with Visual studio
Part 2: Powershell and WPF: Build GUI applications tutorial
Part 3: Create shorter Microsoft Powershell WPF automated clean script
Powershell PoshGUI: Convert user to SID and vice versa using
Microsoft Powershell GUI: Change Internet Options connections Lan settings proxy server grayed out

Go Back

Comment

Blog Search

Page Views

1495540

Follow me on Blogarama