Menu

Virtual Geek

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

Powershell WPF MahApps.Metro update theme library

This script is just an update to new dll libraries of MahApps.Metro theme for Powershell WPF. I had already written basics of  Part 3: Powershell wpf MahApps.Metro theme step by step guide but, It is almost 2 years back and I was using MahApps.Metro 1.6 version and new version of MahApps.Metro v2.4 libraries are released. Which I tested and found stable and smooth compared to earlier version (I am planning to write on new controls). I have updated WPF XAML form to use theme color as I have highlighted in the screenshot.

Microsoft Powershell WPF Mahapps.metro Window.Resources resourcedirectory xmlns xaml metrowindow controls apptheme.png

Next In the binary dll section instead of System.Windows.Interactivity.dll it requires new module Microsoft.Xaml.Behaviors.dll. You can download here working template, or it is also available on the github.com/janviudapi.

XAML Template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<Controls:MetroWindow 
        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:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
		xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <!-- Accent and AppTheme setting -->
				<!-- Light, Dark -->
                <!--Red, Green, Blue, Purple, Orange, Lime, Emerald, Teal, Cyan, Cobalt, Indigo, Violet, Pink, Magenta, Crimson, Amber, Yellow, Brown, Olive, Steel, Mauve, Taupe, Sienna -->
				<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Green.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
        <Label Content="Buttons" HorizontalAlignment="Left" Margin="10,3,0,0" VerticalAlignment="Top" Width="112"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="10,29,0,0" VerticalAlignment="Top" Width="112"/>
        <ToggleButton Content="ToggleButton" HorizontalAlignment="Left" Margin="10,58,0,0" VerticalAlignment="Top" Width="112" IsChecked="True"/>
        <RepeatButton Content="RepeatButton" HorizontalAlignment="Left" Margin="10,85,0,0" VerticalAlignment="Top" Width="112"/>
        <Label Content="CheckBoxes" HorizontalAlignment="Left" Margin="127,3,0,0" VerticalAlignment="Top" Width="112"/>
        <CheckBox Content="CheckBox1" HorizontalAlignment="Left" Margin="127,32,0,0" VerticalAlignment="Top" Height="20"/>
        <CheckBox Content="CheckBox2" HorizontalAlignment="Left" Margin="127,60,0,0" VerticalAlignment="Top" IsChecked="{x:Null}"/>
        <CheckBox Content="CheckBox3" HorizontalAlignment="Left" Margin="127,87,0,0" VerticalAlignment="Top" IsChecked="True"/>
        <Label Content="RadioButtons" HorizontalAlignment="Left" Margin="244,3,0,0" VerticalAlignment="Top" Width="112"/>
        <RadioButton Content="RadioButton1" HorizontalAlignment="Left" Margin="244,32,0,0" VerticalAlignment="Top"/>
        <RadioButton Content="RadioButton2" HorizontalAlignment="Left" Margin="244,61,0,0" VerticalAlignment="Top" IsChecked="True"/>
        <RadioButton Content="RadioButton1" HorizontalAlignment="Left" Margin="244,87,0,0" VerticalAlignment="Top"/>
        <Label Content="CheckBoxes" HorizontalAlignment="Left" Margin="450,3,0,0" VerticalAlignment="Top" Width="112"/>
        <ComboBox Name="combobox1" HorizontalAlignment="Left" Margin="382,29,0,0" VerticalAlignment="Top" Width="120" IsDropDownOpen="True"/>
        <ComboBox HorizontalAlignment="Left" Margin="507,29,0,0" VerticalAlignment="Top" Width="120" IsHitTestVisible="True"/>
        <ComboBox HorizontalAlignment="Left" Margin="507,56,0,0" VerticalAlignment="Top" Width="120" />
        <ComboBox HorizontalAlignment="Left" Margin="507,81,0,0" VerticalAlignment="Top" Width="120" />
        <Label Content="Image" HorizontalAlignment="Left" Margin="632,3,0,0" VerticalAlignment="Top" Width="134"/>
        <Image Name="Image1" HorizontalAlignment="Left" Height="100" Margin="632,29,0,0" VerticalAlignment="Top" Width="134"/>
        <Label Content="DataGrid" HorizontalAlignment="Left" Margin="10,112,0,0" VerticalAlignment="Top" Width="224"/>
        <DataGrid Name="Datagrid" HorizontalAlignment="Left" Height="100" Margin="10,143,0,0" VerticalAlignment="Top" Width="229"/>
        <Label Content="ListBox" HorizontalAlignment="Left" Margin="244,112,0,0" VerticalAlignment="Top" Width="112"/>
        <ListBox Name='ListBox' HorizontalAlignment="Left" Height="100" Margin="244,144,0,0" VerticalAlignment="Top" Width="112"/>
        <Label Content="TabControl" HorizontalAlignment="Left" Margin="693,134,0,0" VerticalAlignment="Top" Width="89"/>
        <TabControl HorizontalAlignment="Left" Height="100" Margin="507,165,0,0" VerticalAlignment="Top" Width="275">
            <TabItem Header="TabItem">
                <Grid>
                    <PasswordBox HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="149" Grid.ColumnSpan="2"/>
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid></Grid>
            </TabItem>
        </TabControl>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="361,183,0,0" TextWrapping="Wrap" Text="TextBox1" VerticalAlignment="Top" Width="141"/>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="361,211,0,0" TextWrapping="Wrap" Text="TextBox2" VerticalAlignment="Top" Width="141"/>
        <Label Content="TextBox" HorizontalAlignment="Left" Margin="361,152,0,0" VerticalAlignment="Top" Width="141"/>
        <ProgressBar HorizontalAlignment="Left" Height="16" Margin="570,324,0,0" VerticalAlignment="Top" Width="212" Value="70"/>
        <StatusBar Name="statusBar1" Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Bottom">
            <StatusBarItem Content="Item 1" Width="75"/>
            <StatusBarItem Content="http://vcloud-lab.com" Width="150" />
            <StatusBarItem HorizontalAlignment="Right">
                <StackPanel Orientation="Horizontal">
                    <StatusBarItem  Content="Item 3" Width="92"/>
                    <StatusBarItem Content="Item 4" Width="114"/>
                    <ProgressBar Height="14" Width="210" IsIndeterminate="True" Margin="5,0"/>
                </StackPanel>
            </StatusBarItem>
        </StatusBar>
        <Label Content="Status Bar" HorizontalAlignment="Left" Margin="570,270,0,0" VerticalAlignment="Top" Width="212"/>
        <ProgressBar HorizontalAlignment="Left" Height="18" Margin="570,301,0,0" VerticalAlignment="Top" Width="212" IsIndeterminate="true"/>
        <DatePicker HorizontalAlignment="Left" Margin="450,296,0,0" VerticalAlignment="Top" Width="115"/>
        <Label Content="Date Picker" HorizontalAlignment="Left" Margin="447,270,0,0" VerticalAlignment="Top" Width="115"/>
        <Expander Header="Expander" HorizontalAlignment="Left" Height="107" Margin="10,248,0,0" VerticalAlignment="Top" Width="139" IsExpanded="True">
            <Button Content="Button" HorizontalAlignment="Left" Margin="28,0,0,0" Width="82" Height="34"/>
        </Expander>
        <Slider HorizontalAlignment="Left" Margin="447,332,0,0" VerticalAlignment="Top" Width="112"/>
        <GroupBox Header="GroupBox" HorizontalAlignment="Left" Height="112" Margin="154,249,0,0" VerticalAlignment="Top" Width="116"/>
        <ScrollBar HorizontalAlignment="Left" Margin="275,257,0,0" VerticalAlignment="Top" Height="104" Width="14"/>
        <ScrollBar HorizontalAlignment="Left" Margin="295,257,0,0" VerticalAlignment="Top" Width="135" Orientation="Horizontal" />
        <Label Content="Scroll Bar" HorizontalAlignment="Left" Margin="295,280,0,0" VerticalAlignment="Top" Width="112"/>
    </Grid>
</Controls:MetroWindow>

Powershell WPF script example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#Load required libraries #http://vcloud-lab.com
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms, System.Drawing 

#Load required dll libraries
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
$assemblyLocation = Join-Path -Path $scriptPath -ChildPath .\binary
foreach ($assembly in (Get-ChildItem $assemblyLocation -Filter *.dll)) {
    [System.Reflection.Assembly]::LoadFrom($assembly.FullName) | out-null
}

#Read XAML form
[xml]$xaml = Get-Content "$PSScriptRoot\xaml\form.xaml"

#Load XAML form
$reader = (New-Object System.Xml.XmlNodeReader $xaml) 
$form = [Windows.Markup.XamlReader]::Load($reader) 

#Load control variables
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach-Object { 
	New-Variable  -Name $_.Name -Value $Form.FindName($_.Name) -Force 
}

#Code & Events
$ComboBox1.ItemsSource =  @('Item1','Item2','Item3')
$combobox1.SelectedItem = 'Item1'
$Image1.Source = "$PSScriptRoot\Images\Image.png"
#$DataGrid.RowBackground = 'Black'
$Datagrid.ItemsSource = Get-Service | Select-Object Name, Status, StartType -First 5
$ListBox.ItemsSource =  @('Item1','Item2','Item3')
$combobox1.SelectedItem = 'Item1'

#Mandetory last line of every script to load form
[void]$Form.ShowDialog()

Useful Articles
COOL POWERSHELL FREE ONLINE GUI GENERATOR TOOL, POSHGUI
Generate random password GUI using powershell
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 WPF Themes guide step by step
Part 2: Powershell WPF Themes guide step by step
Powershell WPF custom Image based ProgressBar

Go Back

Comment

Blog Search

Page Views

12056342

Follow me on Blogarama