title: How to repeat XAML elements with Powershell
body: <p>I have a piece of Poweshell code with XAML and I'm stuck on repeating some XAML items #n of times.</p>
  
  <p>The $domainlist is filled trough the CollectDomains function, this function extracts two items (Hostname and Domainname) from a Excel sheet. The function retuns a list of unique domainnames.</p>
  
  <pre><code>foreach ($domain in $domainlist) { Start function to Repeat XAML code and returm credential objects per unique domainname ... }
  </code></pre>
  
  <p>The code for the row is:</p>
  
  <pre><code>&lt;Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,162,0,0" Stroke="#FFCDCDCD"/&gt;
  &lt;Label x:Name="LblDomain1" Content="Domain:" HorizontalAlignment="Left" Margin="14,166,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/&gt;
  &lt;Label x:Name="LblUsername1" Content="Username:" HorizontalAlignment="Left" Margin="240,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
  &lt;TextBox x:Name="TxtUsername1" HorizontalAlignment="Left" Height="25" Margin="240,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
  &lt;Label x:Name="LblPassword1" Content="Password:" HorizontalAlignment="Left" Margin="433,165,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
  &lt;TextBox x:Name="TxtPassword1" HorizontalAlignment="Left" Height="25" Margin="433,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
  &lt;Label x:Name="LblReuseCreds1" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
  &lt;ComboBox x:Name="CmbReuseCreds1" HorizontalAlignment="Left" Margin="626,190,0,0" VerticalAlignment="Top" Width="188" Height="25"/&gt;
  </code></pre>
  
  <p>I tried to split the Xaml code to $XamlHeader, $XamlContent and $XamlFooter but this doesn't make sense to me. What is the best way repeat the row with XAML items in the WPF GUI??</p>
  
  <p>I've created a short sample script below:</p>
  
  <pre><code>$GuiXaml = @'
      &lt;Window
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              Height="800" Width="1024"&gt;
          &lt;Grid&gt;
              &lt;Rectangle Fill="White" HorizontalAlignment="Left" Height="553" VerticalAlignment="Top" Width="1016" Margin="0,156,0,0" Stroke="#FFCCCCCC"/&gt;
              &lt;Rectangle Fill="#FF007ABE" HorizontalAlignment="Left" Height="80" VerticalAlignment="Top" Width="1016" Margin="0,-1,0,0"/&gt;
              &lt;Rectangle Fill="#FFF0F0F0" HorizontalAlignment="Left" Height="77" VerticalAlignment="Top" Width="1016" Margin="0,79,0,0"/&gt;
              &lt;Label x:Name="GuiScreenTitle" Content="Push script to Excel list servers" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="24" Width="347" Height="42" Margin="10,18,0,0" Foreground="White" FontWeight="SemiBold" HorizontalContentAlignment="Left" FontFamily="Segoe UI Semilight"/&gt;
              &lt;Button x:Name="BtnBrowseXlsx" Content="Browse" HorizontalAlignment="Left" Margin="10,90,0,0" VerticalAlignment="Top" Width="100" Height="25"/&gt;
              &lt;Label x:Name="LblBrowseXlsx" Content="Chose the Excel file to use" HorizontalAlignment="Left" Margin="115,90,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="225"/&gt;
              &lt;Label x:Name="LblLastXlsxFile" Content="Last used: ........\TestServers.xlsx" HorizontalAlignment="Left" Margin="339,90,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="655" FontWeight="Bold"/&gt;
              &lt;Button x:Name="BtnBrowseScript" Content="Browse" HorizontalAlignment="Left" Margin="10,120,0,0" VerticalAlignment="Top" Width="100" Height="25"/&gt;
              &lt;Label x:Name="LblBrowseScript" Content="Chose the script file to push to remotely execute" HorizontalAlignment="Left" Margin="115,120,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="225"/&gt;
              &lt;Label x:Name="LblLastPs1File" Content="Last used: ........\SCCMClientConnectivityTests.ps1" HorizontalAlignment="Left" Margin="339,120,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="655" FontWeight="Bold"/&gt;
              &lt;Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,162,0,0" Stroke="#FFCDCDCD"/&gt;
              &lt;Label x:Name="LblDomain1" Content="Domain:" HorizontalAlignment="Left" Margin="14,166,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/&gt;
              &lt;Label x:Name="LblUsername1" Content="Username:" HorizontalAlignment="Left" Margin="240,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
              &lt;TextBox x:Name="TxtUsername1" HorizontalAlignment="Left" Height="25" Margin="240,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
              &lt;Label x:Name="LblPassword1" Content="Password:" HorizontalAlignment="Left" Margin="433,165,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
              &lt;TextBox x:Name="TxtPassword1" HorizontalAlignment="Left" Height="25" Margin="433,190,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
              &lt;Label x:Name="LblReuseCreds1" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,166,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
              &lt;ComboBox x:Name="CmbReuseCreds1" HorizontalAlignment="Left" Margin="626,190,0,0" VerticalAlignment="Top" Width="188" Height="25"/&gt;
              &lt;Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,231,0,0" Stroke="#FFCDCDCD"/&gt;
              &lt;Label x:Name="LblDomain2" Content="Domain:" HorizontalAlignment="Left" Margin="14,235,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/&gt;
              &lt;Label x:Name="LblUsername2" Content="Username:" HorizontalAlignment="Left" Margin="240,235,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
              &lt;TextBox x:Name="TxtUsername2" HorizontalAlignment="Left" Height="25" Margin="240,259,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
              &lt;Label x:Name="LblPassword2" Content="Password:" HorizontalAlignment="Left" Margin="433,234,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
              &lt;TextBox x:Name="TxtPassword2" HorizontalAlignment="Left" Height="25" Margin="433,259,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
              &lt;Label x:Name="LblReuseCreds2" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,235,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
              &lt;ComboBox x:Name="CmbReuseCreds2" HorizontalAlignment="Left" Margin="626,259,0,0" VerticalAlignment="Top" Width="188" Height="25"/&gt;
              &lt;Rectangle Fill="#FFF0F0F0" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="1016" Margin="0,709,0,0"/&gt;
              &lt;Button x:Name="BtnStart" Content="Start" HorizontalAlignment="Left" Margin="809,723,0,0" VerticalAlignment="Top" Width="100" Height="25"/&gt;
              &lt;Button x:Name="BtnCancel" Content="Exit" HorizontalAlignment="Left" Margin="914,723,0,0" VerticalAlignment="Top" Width="80" Height="25"/&gt;
          &lt;/Grid&gt;
      &lt;/Window&gt;
  '@
  
  Function CollectDomains {   
      $ServerList = Import-XLSX -Path $ImportFile -Header Hostname, Domain, RemotePowershell, RemoteBatchfile
      $DomainResults = @()
  
      Foreach ($ExcelDomain in ($ServerList.Domain | sort-object | Get-Unique)) {
          $ExcelDomainItem = New-Object PSObject
          $ExcelDomainItem | Add-Member -type NoteProperty -Name 'DomainName' -Value $ExcelDomain
          $DomainResults += $ExcelDomainItem
      }
  
      Return $DomainResults
  }
  
  function ShowLoginScreen {
  
  $DomainList = CollectDomains
  
  Foreach ($Domain in $DomainList) {
  $XamlText = @'
      &lt;Rectangle Fill="#FFF1F1F1" HorizontalAlignment="Left" Height="64" VerticalAlignment="Top" Width="997" Margin="6,$c,0,0" Stroke="#FFCDCDCD"/&gt;
      &lt;Label x:Name="LblDomain$b" Content="Domain:" HorizontalAlignment="Left" Margin="14,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="49" Width="221"/&gt;
      &lt;Label x:Name="LblUsername$b" Content="Username:" HorizontalAlignment="Left" Margin="240,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
      &lt;TextBox x:Name="TxtUsername$b" HorizontalAlignment="Left" Height="25" Margin="240,$c,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
      &lt;Label x:Name="LblPassword$b" Content="Password:" HorizontalAlignment="Left" Margin="433,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
      &lt;TextBox x:Name="TxtPassword$b" HorizontalAlignment="Left" Height="25" Margin="433,$c,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="188" Padding="3"/&gt;
      &lt;Label x:Name="LblReuseCreds$b" Content="Use credentials from domain:" HorizontalAlignment="Left" Margin="626,$c,0,0" VerticalAlignment="Top" FontSize="10" Height="25" Width="188"/&gt;
      &lt;ComboBox x:Name="CmbReuseCreds$b" HorizontalAlignment="Left" Margin="626,$c,0,0" VerticalAlignment="Top" Width="188" Height="25"/&gt;
  '@
  }
  
  CreateGui -Xaml $GuiXaml
  
  $Gui | % {
      $_.Title = "Voer inlog gegevens in"
      $_.WindowStartupLocation ="CenterScreen"
      $_.ShowInTaskbar = "True"
      $_.ResizeMode ="NoResize"
      $_.Add_KeyDown({
          if ($args[1].key -eq 'Return') { $Gui.Close(); StartTests }
          elseif ($args[1].key -eq 'Escape') { $Gui.Close(); Exit }
      })
  }
  
  $BtnStart.Add_Click({ $Gui.Close(); StartTests })
  $BtnCancel.Add_Click({ $Gui.Close(); Exit })
  
  $Gui.Showdialog() | Out-Null
  
  }
  
  ShowLoginScreen
  
  # Remove leftover functions
  Remove-Item function:\add*
  </code></pre>