Powershell WPF点击事件处理程序存在问题

时间:2018-11-30 15:50:39

标签: wpf forms powershell click handler

我有一个带有复选框网格的表单。 当用户选中或取消选中一行中最左边的复选框时,我希望它选中或取消选中该行中的所有其他复选框。 我想将click事件处理程序添加到最左边2个复选框。 我想循环执行此操作(而不是单独执行),因为行数未知。

预先感谢您的任何建议。

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

0 个答案:

没有答案