function Show-XAMLWindow{ [CmdletBinding( SupportsShouldProcess = $false, ConfirmImpact = "none", DefaultParameterSetName = "" )] param ( [Parameter( HelpMessage = "Enter tbind xaml @`...`@ to load.", Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [ValidateNotNullOrEmpty()] [string] $xaml, [Alias("PassThru")] [switch] $PassThrough ) begin { try { Add-Type -AssemblyName presentationframework } catch { } try { [xml]$xaml = $xaml } catch { } $reader=(New-Object System.Xml.XmlNodeReader $xaml) $Form=[Windows.Markup.XamlReader]::Load( $reader ) #Setting al the fields to varialbe $cbCompanys = $Form.FindName("cbCompanys") $chkbMultiSelect = $Form.FindName("chkbMultiSelect") $dgUsers = $Form.FindName("dgUsers") $chkbBackupFiles = $Form.FindName("chkbBackupFiles") $chkbBackupMail = $Form.FindName("chkbBackupMail") $chkblocation = $Form.FindName("chkblocation") $btnSend = $Form.FindName("btnSend") } process { # Form setup #$Form.Background = $Global:BackColor # for now disable the multiselect button $chkbMultiSelect.IsEnabled = $false # Add scripts to Company Combobox #foreach ($Company in ($Global:Companys | sort Name)) { # [void] $cbCompanys.Items.Add($Company.Name) #} [void] $cbCompanys.Items.Add("A") [void] $cbCompanys.Items.Add("B") # Add on checked to CheckBox Multiselect $chkbMultiSelect.Add_Checked({ $dgUsers.SelectionMode = "Extended" }) # Add on UnChecked to CheckBox Multiselect $chkbMultiSelect.Add_UnChecked({ $dgUsers.SelectionMode = "Single" }) # Add onchange to Company Combobox $cbCompanys_Add_SelectionChanged = { # Disable User Combobox $cbCompanys.IsEnabled = $False # Clear the Users Combobox $dgUsers.Clear() # Get all users of company #$CompUsers = Get-CompanyUser -Company $cbCompanys.SelectedItem | select DisplayName, UserPrincipalName, SamAccountName | sort DisplayName $CompUsers = @( [pscustomobject]@{ DisplayName="Andy"; UserPrincipalName="Andy P"; SamAccountName="Andy S";} [pscustomobject]@{ DisplayName="Bob"; UserPrincipalName="Bob P"; SamAccountName="Bob S"; } [pscustomobject]@{ DisplayName="Casey"; UserPrincipalName="Casey P"; SamAccountName="Casey S"; } ) # Add property for checkbox $CompUsers | ForEach-Object { $_ | Add-Member -MemberType NoteProperty -Name IsChecked -Value $False -TypeName System.Boolean; } # Add users to DataGrid $dgUsers.ItemsSource = $CompUsers # Allow sorting on all columns $dgUsers.Columns | ForEach-Object { $_.CanUserSort = $True $_.IsReadOnly = $True } # Set Columns ReadOnly of not $dgUsers.Columns[0].IsReadOnly = $False # Enable User Combobox $cbCompanys.IsEnabled = $True } # Add selectionchanged function to Combobox $cbCompanys.Add_SelectionChanged($cbCompanys_Add_SelectionChanged) $cbCompanys.SelectedValue = "A" # Set action to button $btnSend.Add_Click({ # Get selected items $SelCompany = $cbCompanys.SelectedItem ### @{DisplayName=Andy; UserPrincipalName=Andy P; SamAccountName=Andy S; IsChecked=False} ### @{DisplayName=Bob; UserPrincipalName=BobP; SamAccountName=Bob S; IsChecked=True} ### @{DisplayName=Casey; UserPrincipalNam=Casey P; SamAccountName=Casey S; IsChecked=False} $iRow = 0 # Checking if all fiels contain value if (!($SelCompany)) { EmptyFormField -Field "Company" } else { # write-host "----" # write-host $dgUsers ### System.Windows.Controls.DataGrid Items.Count:3 # write-host "----" # write-host $dgUsers.ItemsSource # write-host "----" $dgUsers.ItemsSource | ForEach-Object { $myRow = $dgUsers.ItemContainerGenerator.ContainerFromItem($_) if($_.IsChecked -eq $True ){ $myRow.Background = ([System.Windows.Media.Brushes]::Green) } else{ $myRow.Background = ([System.Windows.Media.Brushes]::White) } #$myRow.Background = ([System.Windows.Media.Brushes]::Green) } } }) ### LOOK AT HERE ### - start $dgUsers.Add_LoadingRow({ param($Sender,$EventArgs) #write-host "########### LOADED!!!!##########" #write-host $EventArgs.Row.Item.IsChecked if($EventArgs.Row.Item.IsChecked -eq $True ){ $EventArgs.Row.Background = ([System.Windows.Media.Brushes]::Green) } else{ $EventArgs.Row.Background = ([System.Windows.Media.Brushes]::White) } }) ### LOOK AT HERE ### - stop $Form.ShowDialog() | out-null } end { return $dgUsers.ItemsSource | Where-Object {$_.IsChecked -eq $True} } } $xaml =@'