I'm trying to create a form with XAML. The problem i'm facing is that i cant retrieve the value's from a datagrid. What i want is to loop true al the checked checkboxes.
The code is use is:
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">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" CanUserSort="False" CanUserResize="False" IsReadOnly="False"/>
</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="Send" Margin="491,469,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>
Powershell:
function Remove-GuiCompanyUser {
begin {
#Xaml Path
[xml]$xaml = Get-Content "$Global:relPathXaml\RemoveCompanyUser.xaml"
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
# Try to read the XAML to form
try {
$Form=[Windows.Markup.XamlReader]::Load( $reader )
} catch {
Write-Host "Something went wrong..."
}
#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)
}
# 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
# 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)
# Set action to button
$btnSend.Add_Click({
# Get selected items
$SelCompany = $cbCompanys.SelectedItem
# Checking if all fiels contain value
if (!($SelCompany)) {
EmptyFormField -Field "Company"
} else {
for($i=0;$i -lt $dgUsers.items.count;$i++){
if($dgUsers.Rows[$i].Cells[0].Value -eq $true)
{
write-host "cell #$i is checked"
}
}
}
})
# Start form
$Form.ShowDialog() | out-null
}
end {
}
}
After some googleing i found out you can call the rows by $dgUsers.Rows but that value doesn't exist. $dgUsers.Columns does exist.. I can call al my value's with $dgUsers.Items. But that only gives the array back I put in to it. So i don't have the checkboxes..
Anyone an idea?
My powershell version is:
Major Minor Build Revision
----- ----- ----- --------
4 0 30319 34209
Greetings, Jos Vink