需要Powershell脚本的ListBox事件处理程序

我正在尝试使用XAML对象制作powershellscript,该对象可用于配置AD用户。 我现在的目标是为特定的文本框添加实时搜索。该脚本使用文本框中的输入,将其与AD用户进行比较,然后在列表框中显示输出。我需要的是一个事件处理程序,当我单击列表框中的任何项目时即可激活

最后一部分是相关的部分。 感谢您的帮助。

        $inputXML = @"
    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication3"
            mc:Ignorable="d"
            Title="HHI Helpdesk Helper" Height="350" Width="654.604" Icon="C:\Users\hoesl\Pictures\logo.png" WindowStartupLocation="CenterScreen">
        <Grid Background="#FFA0A0A0">
            <TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="301" Margin="10,10,0" VerticalAlignment="Top" Width="628">
                <TabItem Header="Enable Admin account">
                    <Grid Background="#FFE5E5E5">
                        <Label x:Name="label" Content="User : " HorizontalAlignment="Left" Margin="47,0" VerticalAlignment="Top"/>
                        <Label x:Name="label1" Content="Service-Tag : " HorizontalAlignment="Left" Margin="10,53,0" VerticalAlignment="Top"/>
                        <Label x:Name="label2" Content="Set Password : " HorizontalAlignment="Left" Margin="2,95,0" VerticalAlignment="Top"/>
                        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="95,57,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="95,99,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <Calendar HorizontalAlignment="Left" Margin="434,0" VerticalAlignment="Top"/>
                        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,127,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="116" Width="410">
                            <TextBlock.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="Black" Offset="0"/>
                                    <GradientStop Color="#FFA3A3A3"/>
                                </LinearGradientBrush>
                            </TextBlock.Background>
                        </TextBlock>
                        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="434,182,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" RenderTransformOrigin="0.482,0.542" Height="27" Width="178" OpacityMask="White">
                            <TextBlock.Background>
                                <RadialGradientBrush>
                                    <GradientStop Color="Black" Offset="0"/>
                                    <GradientStop Color="#FFE0DFDF"/>
                                </RadialGradientBrush>
                            </TextBlock.Background>
                        </TextBlock>
                        <Label x:Name="label3" Content="Valid Until : " HorizontalAlignment="Left" Margin="346,11,0" VerticalAlignment="Top"/>
                        <CheckBox x:Name="checkBox" Content="Keep old password" HorizontalAlignment="Left" Margin="220,108,0" VerticalAlignment="Top"/>
                        <Button x:Name="button" Content="Apply" HorizontalAlignment="Left" Margin="435,215,0" VerticalAlignment="Top" Width="178" Height="29" BorderBrush="White" Background="#FFA6A5A5"/>
                        <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="125" Margin="95,32,-0.2" VerticalAlignment="Top" Width="120" Visibility="Hidden" IsSynchronizedWithcurrentitem="True"/>
                        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="95,14,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />

                    </Grid>
                </TabItem>
                <TabItem Header="Re-activate Admin account">
                    <Grid Background="#FFE5E5E5">
                        <Label x:Name="label4" Content="User : " HorizontalAlignment="Left" Margin="85,0" VerticalAlignment="Top"/>
                        <TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="23" Margin="133,13,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <Calendar HorizontalAlignment="Left" Margin="420,0" VerticalAlignment="Top" Height="168" Width="188"/>
                        <Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="420,214,0" VerticalAlignment="Top" Width="193" Height="29"/>
                    </Grid>
                </TabItem>
                <TabItem Content="New User" Header="New User"/>
                <TabItem Header="Unlock User"/>
                <TabItem Header="New Extern User">
                    <Grid Background="#FFE5E5E5">
                        <TextBox x:Name="textBox4" HorizontalAlignment="Left" Height="23" Margin="111,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <TextBox x:Name="textBox5" HorizontalAlignment="Left" Height="23" Margin="111,49,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <TextBox x:Name="textBox6" HorizontalAlignment="Left" Height="23" Margin="111,88,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <TextBox x:Name="textBox7" HorizontalAlignment="Left" Height="23" Margin="111,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <TextBox x:Name="textBox8" HorizontalAlignment="Left" Height="23" Margin="111,167,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
                        <Label x:Name="label5" Content="Firstname :" HorizontalAlignment="Left" Margin="36,0" VerticalAlignment="Top"/>
                        <Label x:Name="label6" Content="Lastname :" HorizontalAlignment="Left" Margin="36,45,0" VerticalAlignment="Top"/>
                        <Label x:Name="label7" Content="E-Mail-Address :" HorizontalAlignment="Left" Margin="6,85,0" VerticalAlignment="Top"/>
                        <Label x:Name="label8" Content="Manager :" HorizontalAlignment="Left" Margin="39,124,0" VerticalAlignment="Top"/>
                        <Label x:Name="label9" Content="Ticketnumber :" HorizontalAlignment="Left" Margin="15,163,0" VerticalAlignment="Top"/>
                        <TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="295,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="175" Width="318" Background="#FF323131" Foreground="White" Cursor="Wait"/>
                        <Button x:Name="button2" Content="Button" HorizontalAlignment="Left" Margin="503,212,0" VerticalAlignment="Top" Width="110" Height="31"/>

                    </Grid>
                </TabItem>
            </TabControl>

        </Grid>
    </Window>

    "@ 

    $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*','<Window'
    [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    [xml]$XAML = $inputXML
    #Read XAML

    $reader=(New-Object system.xml.XmlNodeReader $xaml)
    try{
        $Form=[Windows.Markup.XamlReader]::Load( $reader )
    }
    catch{
        Write-Warning "Unable to parse XML,with error: $($Error[0])`n Ensure that there are NO SelectionChanged or TextChanged properties in your textboxes (PowerShell cannot process them)"
        throw
    }

    #===========================================================================
    # Load XAML Objects In PowerShell
    #===========================================================================

    $xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)";
        try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.findname($_.Name) -Erroraction Stop}
        catch{throw}
        }

    Function Get-FormVariables{
    if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again,run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
    write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
    get-variable WPF*
    }

    Get-FormVariables

    #===========================================================================
    # Use this space to add code to the various form elements in your GUI
    #===========================================================================

    #setzt die Variablen auf bestimmte Objekte,wodurch das Verändern der Objekte sehr flexibel ist
    $OutputBox = $Form.findname('textBlock')
    $InputBox = $Form.findname('textBox')
    $ListBox = $Form.findname('listBox')


    #Eventhandler
    $InputBox.Add_TextChanged({
        $input = $InputBox.Text


        if($input.length -gt 3){
        $ListBox.Visibility ="Visible"
        $input = "*" + $input + "*"
        $user = @(Get-ADUser -Filter ' Name -like  $input ' | Select -ExpandProperty Name)
        #debug $OutputBox.Text = ($user | ForEach {"{0}`r" -f $_})
        $ListBox.itemsSource = $user
---> Here should be the eventhandler for clicking an item in the listbox <---
        # $listuser = Get-ADUser -Filter ' Name -like  $ListBox.SelectedItem '
        # $OutputBox.Text = $ListBox.SelectedItem

        }

    })





    $Form.ShowDialog()
hezhen2009 回答:需要Powershell脚本的ListBox事件处理程序

答案:

 $ListBox.add_SelectionChanged({$OutputBox.Text = $ListBox.SelectedItem}) 


我个人更喜欢使用事件创建特殊函数,并在$Form.ShowDialog()之前调用它。根据需要添加尽可能多的事件。示例:

         function event_handler
        {
            $textbox_name.add_TextChanged({$textbox_Login.Text = Construct-Login $textbox_name.Text $textbox_LastName.Text})

        }

在此示例中,当您在文本框textbox_name中键入内容时,将调用函数Construct-Login,并将结果写入另一个文本框。

顺便说一句,您不需要额外的

$OutputBox = $Form.FindName('textBlock')
$ListBox = $Form.FindName('listBox') 

因为$xaml.SelectNodes为您代劳。只要确保您的表单上所有控件都有唯一的名称即可。

本文链接:https://www.f2er.com/3161319.html

大家都在问