WPF Tips

2020/09/29 | 4 minute read |

I will start some post around WPF and PowerShell with XAML and I give you some Tips for your applications Today I start this series with Variables in your project. the First question is “How to find variables into your XAML files ?”

There is multiple ways, for example we start with the same XAML file.

<Controls:MetroWindow
   Name="MainPanel"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
   xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
   xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
   xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
   xmlns:mde="clr-namespace:MaterialDesignExtensions.Controls;assembly=MaterialDesignExtensions"
   xmlns:model="clr-namespace:MaterialDesignExtensions.Model;assembly=MaterialDesignExtensions"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Width="950"
   Height="420"
   BorderThickness="1"
   GlowBrush="{DynamicResource AccentColorBrush}"
   RenderOptions.ClearTypeHint="Enabled"
   TextOptions.TextFormattingMode="Display"
   Title="WPF Tips - Variables"
   WindowStartupLocation="CenterScreen"
   WindowStyle="None">
   <Window.Resources>
      <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/Generic.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignExtensions;component/Themes/Generic.xaml"/>
            <materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="Indigo" SecondaryColor="Lightblue"/>
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
   </Window.Resources>
   <Controls:MetroWindow.LeftWindowCommands>
      <Controls:WindowCommands>
         <Button Name="Github" ToolTip="Github Link JM2K69">
            <StackPanel Orientation="Horizontal">
               <iconPacks:PackIconFontAwesome Kind="GithubBrands"/>
            </StackPanel>
         </Button>
      </Controls:WindowCommands>
   </Controls:MetroWindow.LeftWindowCommands>
   <Grid>
      <StackPanel HorizontalAlignment="Center" Margin="10" VerticalAlignment="Center">
         <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
            <StackPanel Orientation="Vertical">
               <StackPanel Margin="0,0,0,15" Orientation="Horizontal">
                  <Label Content="WPF" FontSize="30" Foreground="#FF444444">
                  </Label>
                  <Label
                     Margin="15 0 0 0"
                     Content="--"
                     FontSize="30"
                     Foreground="#FF444444">
                  </Label>
                  <Label
                     Margin="15 0 0 0"
                     Content="Tips"
                     FontSize="30"
                     Foreground="#5980F1">
                  </Label>
               </StackPanel>
            </StackPanel>
         </StackPanel>
         <StackPanel Orientation="Horizontal">
            <StackPanel Orientation="Vertical">
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconMaterial Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Wan"/>
                  <Label Width="110" Margin="4,0,0,0" Content="Network type">
                  </Label>
                  <ComboBox
                     Name="Choose_Network"
                     Width="185"
                     Height="25"
                     Margin="10,0,0,0"
                     SelectedIndex="0"
                     Text="Choose">
                     <ComboBoxItem Name="Choose_Static" Content="Static">
                     </ComboBoxItem>
                  </ComboBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconModern Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Network"/>
                  <Label Width="110" Margin="4,0,0,0" Content="IP Address ">
                  </Label>
                  <TextBox
                     Name="IPAddress"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     IsReadOnly="True">
                  </TextBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconMaterial Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Gate"/>
                  <Label Width="110" Margin="4,0,0,0" Content="Gateway">
                  </Label>
                  <TextBox
                     Name="Gateway"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     Controls:TextBoxHelper.ClearTextButton="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextBoxHelper.HasText), Mode=OneWay}">
                  </TextBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconEntypo Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Mask"/>
                  <Label Width="110" Margin="4,0,0,0" Content="Network Mask">
                  </Label>
                  <TextBox
                     Name="Network_Mask"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0">
                  </TextBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconMaterial Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Dns"/>
                  <Label Width="110" Margin="4,5,0,0" Content="DNS Server(s)">
                  </Label>
                  <TextBox
                     Name="DNS_Server"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     Controls:TextBoxHelper.ClearTextButton="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextBoxHelper.HasText), Mode=OneWay}">
                  </TextBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconFontAwesome Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="CogsSolid"/>
                  <Label Width="110" Margin="4,0,0,0" Content="Deployment Type">
                  </Label>
                  <TextBox
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     IsEnabled="False"
                     Text="embedded">
                  </TextBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconFontAwesome Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="CogsSolid"/>
                  <Label Width="110" Margin="4,0,0,0" Content="SSH">
                  </Label>
                  <ComboBox
                     Name="SSH"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     SelectedIndex="0"
                     Text="Choose">
                     <ComboBoxItem>True</ComboBoxItem>
                     <ComboBoxItem> False</ComboBoxItem>
                  </ComboBox>
               </StackPanel>
            </StackPanel>
            <StackPanel Margin="15 2 0 0" Orientation="Vertical">
               <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconMaterial Margin="0 8 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Domain"/>
                  <Label Width="110" Margin="4,5,0,0" Content="Site Name">
                  </Label>
                  <TextBox
                     Name="Site_Name"
                     Width="185"
                     Height="27"
                     Margin="10,7,0,0"
                     Controls:TextBoxHelper.ClearTextButton="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextBoxHelper.HasText), Mode=OneWay}"
                     Controls:TextBoxHelper.Watermark="JM2K69">
                  </TextBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconMaterial Margin="0 7 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Domain"/>
                  <Label Width="110" Margin="4,5,0,0" Content="Domain Name">
                  </Label>
                  <TextBox
                     Name="Domain_Name"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     Controls:TextBoxHelper.ClearTextButton="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextBoxHelper.HasText), Mode=OneWay}"
                     Controls:TextBoxHelper.Watermark="vsphere.local">
                  </TextBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconFontAwesome Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="CogsSolid"/>
                  <Label Width="110" Margin="4,0,0,0" Content="CEIP">
                  </Label>
                  <ComboBox
                     Name="CEIP"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     SelectedIndex="0"
                     Text="Choose">
                     <ComboBoxItem> True</ComboBoxItem>
                     <ComboBoxItem> False</ComboBoxItem>
                  </ComboBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconZondicons Margin="0 5 0 0" Foreground="{DynamicResource MahApps.Brushes.Accent}" Kind="Time"/>
                  <Label Width="110" Margin="4,0,0,0" Content="NTP">
                  </Label>
                  <ComboBox
                     Name="NTP"
                     Width="185"
                     Height="27"
                     Margin="10,0,0,0"
                     SelectedIndex="0"
                     Text="Choose">
                     <ComboBoxItem>0.europe.pool.ntp.org</ComboBoxItem>
                     <ComboBoxItem>1.europe.pool.ntp.org</ComboBoxItem>
                     <ComboBoxItem>2.europe.pool.ntp.org</ComboBoxItem>
                     <ComboBoxItem>3.europe.pool.ntp.org</ComboBoxItem>
                     <ComboBoxItem>0.us.pool.ntp.org</ComboBoxItem>
                     <ComboBoxItem>1.us.pool.ntp.org</ComboBoxItem>
                     <ComboBoxItem>2.us.pool.ntp.org</ComboBoxItem>
                     <ComboBoxItem>3.us.pool.ntp.org</ComboBoxItem>
                  </ComboBox>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconModern
                     Width="20"
                     Margin="0 5 0 0"
                     Foreground="{DynamicResource MahApps.Brushes.Accent}"
                     Kind="InterfacePassword"/>
                  <Label Width="90" Margin="4,0,0,0" Content="Root Password">
                  </Label>
                  <PasswordBox
                     Name="Passwd_R"
                     Width="185"
                     Height="27"
                     Margin="25,0,0,0"
                     Controls:TextBoxHelper.ClearTextButton="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextBoxHelper.HasText), Mode=OneWay}"
                     Controls:TextBoxHelper.IsWaitingForData="True"
                     Controls:TextBoxHelper.Watermark="Root Password"
                     PasswordChar="*"
                     Style="{StaticResource MahApps.Styles.PasswordBox.Button.Revealed}"/>
               </StackPanel>
               <StackPanel Margin="0,5,0,0" FlowDirection="LeftToRight" Orientation="Horizontal">
                  <iconPacks:PackIconModern
                     Width="20"
                     Margin="0 5 0 0"
                     Foreground="{DynamicResource MahApps.Brushes.Accent}"
                     Kind="InterfacePassword"/>
                  <Label Width="95" Margin="4,0,0,0" Content="VCSA Password">
                  </Label>
                  <PasswordBox
                     Name="Passwd_VCSA"
                     Width="185"
                     Height="27"
                     Margin="20,0,0,0"
                     Controls:TextBoxHelper.ClearTextButton="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextBoxHelper.HasText), Mode=OneWay}"
                     Controls:TextBoxHelper.IsWaitingForData="True"
                     Controls:TextBoxHelper.Watermark="VCSA Password"
                     PasswordChar="*"
                     Style="{StaticResource MahApps.Styles.PasswordBox.Button.Revealed}"/>
               </StackPanel>
            </StackPanel>
         </StackPanel>
      </StackPanel>
   </Grid>
</Controls:MetroWindow>

Load_assembly

1. The First way

I decided to use Mahapps theme for this Application, because it’s very easy to use with XAML and PowerShell.

The first way to find your WPF object in your form you need use this method : foreach WPF Controls you need to assign and create a variable for add event.

$CEIP = $Form.FindName("CEIP")
$Choose_Network = $Form.FindName("Choose_Network")
$Choose_Static = $Form.FindName("Choose_Static")
$DNS_Server = $Form.FindName("DNS_Server")
$Domain_Name = $Form.FindName("Domain_Name")
$Gateway = $Form.FindName("Gateway")
$Github = $Form.FindName("Github")
$IPAddress = $Form.FindName("IPAddress")
$MainPanel = $Form.FindName("MainPanel")
$Network_Mask = $Form.FindName("Network_Mask")
$NTP = $Form.FindName("NTP")
$Passwd_R = $Form.FindName("Passwd_R")
$Passwd_VCSA = $Form.FindName("Passwd_VCSA")
$Site_Name = $Form.FindName("Site_Name")
$SSH = $Form.FindName("SSH")

2. The second

The second way is very funny because you must just use two function to find in your form all WPF Controls with the atribute Name and create a Powershell variable with this rule $WPF_YourNameControls

$XamlMainWindow.SelectNodes("//*[@Name]") | %{
    try {Set-Variable -Name "$("WPF_"+$_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
    catch{throw}
    }

This Powershell code Find all Controls in your XAML and create a powershell variable.

Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable *WPF*
}
Get-FormVariables

Auhtors_img

Here the result when you launch pour PowerShell script if you want to hide this outpout you need to comment this line like this…

#Get-FormVariables

3. How to try

Auhtors_img

My project is available on Github here

Thank’s for reading.

Written by Jérôme Bezet-Torres @JM2K69.