Проблема с обработчиком событий щелчка Powershell WPF - PullRequest
0 голосов
/ 30 ноября 2018

У меня есть форма с сеткой флажков.Когда пользователь устанавливает или снимает флажок в левом крайнем углу строки, я хочу, чтобы он отмечал или снимал все остальные флажки в строке.Я хочу добавить обработчик события щелчка к двум крайним левым флажкам.Я хочу сделать это в цикле (а не по отдельности), потому что там будет неизвестное количество строк.

Заранее спасибо за любые предложения.

function MarkRow_Click
{
    Param
    (
    [Parameter(Mandatory=$true, Position=0)]
    [System.Windows.Controls.Grid] $Grid,
    [Parameter(Mandatory=$true, Position=1)]
    [System.Windows.Controls.CheckBox] $MarkRowCheckbox

    )

# the name has the row number, get it and use it to get
# all the check boxes for that row in the grid
# then set all the check boxes in that row to match the 'mark all' check box

    $s = 'chk_' + $MarkRowCheckbox.Name.substring(4,2) + '*'

    $ExtraCheckBoxes=$Grid.Children | Where-Object {$_.Name -like $s} 

    ForEach ($ExtraCheckBox in $ExtraCheckBoxes)
        {
        $ExtraCheckBox.ischecked= $MarkRowCheckbox.IsChecked
        }
}


# create a simple example form
# the name of the check box is chk_(row)_(column)

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = @'
<?xml version="1.0" encoding="utf-8"?>
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="wpftest5forpost"
    Width="566"
    Height="329">
    <Grid
        Width="530"
        Height="275"
        Name="gridMain">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <CheckBox
            Width="67"
            Height="34"
            Grid.Column="0"
            Grid.Row="0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="30,40,0,0"
            Name="chk_00_00" />
        <CheckBox
            Width="67"
            Height="34"
            Grid.Column="1"
            Grid.Row="0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="30,40,0,0"
            Name="chk_00_01" />
        <CheckBox
            Width="67"
            Height="34"
            Grid.Column="2"
            Grid.Row="0"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="30,40,0,0"
            Name="chk_00_02" />
        <CheckBox
            Width="67"
            Height="34"
            Grid.Column="0"
            Grid.Row="1"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="30,40,0,0"
            Name="chk_01_00" />
        <CheckBox
            Width="67"
            Height="34"
            Grid.Column="1"
            Grid.Row="1"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="30,40,0,0"
            Name="chk_01_01" />
        <CheckBox
            Width="67"
            Height="34"
            Grid.Column="2"
            Grid.Row="1"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Margin="30,40,0,0"
            Name="chk_01_02" />
    </Grid>
</Window>

'@

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader. Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta, invalid XAML code was encountered."; exit}

$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}


# This is what I want to do, but it doesn't work

<# #>
$checkboxes=$gridMain.Children | Where-Object {$_.Name -like "chk_??_00"}
foreach ($checkbox in $checkboxes)
{
$checkbox.add_Click({MarkRow_Click $gridMain $checkbox})
}
<# #>

# this is a copy of the above code with the individual check box names hardcoded
# and most importantly, the variable names are different
# it works, but I can't code that for every check box.

<#
$checkboxes=$gridMain.Children | Where-Object {$_.Name -like "chk_00_00"}
foreach ($checkbox in $checkboxes)
{
$checkbox.add_Click({MarkRow_Click $gridMain $checkbox})
}


$checkboxes2=$gridMain.Children | Where-Object {$_.Name -like "chk_01_00"}
foreach ($checkbox2 in $checkboxes2)
{
$checkbox2.add_Click({MarkRow_Click $gridMain $checkbox2})
}
#>


$Form.ShowDialog() | out-null
...