pi/tests/function Show-XAMLWindow.ps1

222 lines
8.8 KiB
PowerShell
Raw Permalink Normal View History

2024-10-04 09:16:38 +02:00
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 =@'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window_GuiManagement" Title="Remove Company User" WindowStartupLocation = "CenterScreen"
Width = "587.307" Height = "540.023" Visibility="Visible" WindowStyle="ToolWindow" ResizeMode="NoResize" Topmost="True">
<Grid>
<Label x:Name="lblCompany" Content="Company:" HorizontalAlignment="Left" Margin="11,10,0,0" Height="26" VerticalAlignment="Top"/>
<ComboBox x:Name="cbCompanys" Margin="10,36,10,0" VerticalAlignment="Top"/>
<Label x:Name="lbluser" Content="User:" HorizontalAlignment="Left" Margin="11,63,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="chkbMultiSelect" Content="MultiSelect" Margin="481,69,10,0" VerticalAlignment="Top" Width="90"/>
<DataGrid x:Name="dgUsers" Margin="11,94,10,0" VerticalAlignment="Top" Height="299" SelectionMode="Single" AlternationCount="1"
AutoGenerateColumns="false">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsChecked,UpdateSourceTrigger=PropertyChanged}" ClipboardContentBinding="{x:Null}" CanUserSort="False" CanUserResize="False" IsReadOnly="False"
Header="Check" >
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox"/>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn Binding="{Binding Path=DisplayName}" ClipboardContentBinding="{x:Null}" CanUserSort="False" CanUserResize="False" IsReadOnly="True"
Header="DisplayName"/>
<DataGridTextColumn Binding="{Binding Path=UserPrincipalName}" ClipboardContentBinding="{x:Null}" CanUserSort="False" CanUserResize="False" IsReadOnly="True"
Header="UserPrincipalName"/>
<DataGridTextColumn Binding="{Binding Path=SamAccountName}" ClipboardContentBinding="{x:Null}" CanUserSort="False" CanUserResize="False" IsReadOnly="True"
Header="SamAccountName"/>
</DataGrid.Columns>
</DataGrid>
<CheckBox x:Name="chkbBackupFiles" Content="Backup Files" HorizontalAlignment="Left" Margin="11,398,0,0" VerticalAlignment="Top" IsChecked="True"/>
<CheckBox x:Name="chkbmail" Content="Backup Mail" HorizontalAlignment="Left" Margin="11,418,0,0" VerticalAlignment="Top" IsChecked="True"/>
<CheckBox x:Name="chkblocation" Content="Open backup location" HorizontalAlignment="Left" Margin="11,438,0,0" VerticalAlignment="Top" IsChecked="True"/>
<Button x:Name="btnSend" Content="Color Cecked rows" Margin="441,459,10,0" VerticalAlignment="Top"/>
<CheckBox x:Name="chkblocation_Copy" Content="Open backup location" HorizontalAlignment="Left" Margin="11,438,0,0" VerticalAlignment="Top" IsChecked="True"/>
</Grid>
</Window>
'@
$WPFresult = Show-XAMLWindow -xaml $xaml
$WPFresult