locked
SelectedIndexChange on ComboBox not working RRS feed

  • Question

  • Hi,

    I've created a GUI for my PowerShell script using WPF in Visual Studio.

    I'm trying to capture when a user chooses an item from a combobox and then connect to the vCenter chosen. I am trying to start simple and just have the text box reflect what was chosen. I need this to happen before the user clicks on the button.

    I can't for the life of me get it to work. I know I'm doing something wrong, and I'm hoping someone here can point out my error.

    I added my XAML at the bottom, but it is kept in a separate file.

    #this script automates the decomm process of virtual servers Add-Type -AssemblyName PresentationFramework # where is the XAML file? $XamlPath = "$PSScriptRoot\MainWindow.xaml" [xml]$Form = Get-Content -Path $XamlPath $NR = (New-Object System.Xml.XmlNodeReader $Form) $Win = [WIndows.Markup.XamlReader]::Load($NR) $server = $win.FindName("cmbServer") $vCenter = $win.FindName("cmbvCenter") $btnDecomm = $win.FindName("btnDecomm") $statusBar = $win.FindName("txtStatusBar") $output = $win.FindName("txtOutput") $vCenter.Items.Add("vcenter1") $vCenter.Items.Add("vcenter2") $vCenter.Items.Add("vcenter3") $vCenter_SelectedIndexChanged = { $output.text = "vCenter chosen" write-host "vCenter chosen" } $btnDecomm.Add_Click({ decomm }) $win.ShowDialog()

    <Window 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            Title="Decommission Server" Height="450" Width="465.818"><Grid Height="499">
    <Label Content="Server Name" HorizontalAlignment="Left" Height="32" Margin="19,76,0,0" VerticalAlignment="Top" Width="83"/>
            <Label Content="Select vCenter" HorizontalAlignment="Left" Height="32" Margin="19,39,0,0" VerticalAlignment="Top" Width="101"/>
            <ComboBox Name="cmbvCenter" HorizontalAlignment="Left" Margin="125,39,0,0" VerticalAlignment="Top" Width="287" IsSynchronizedWithCurrentItem="True"/>
            <Button Name="btnDecomm" Content="Decommission Server" HorizontalAlignment="Left" Margin="242,361,0,0" VerticalAlignment="Top" Width="170"/>
            <CheckBox Name="chkCreds" Content="Click here to specify different credentials" HorizontalAlignment="Left" Margin="125,109,0,0" VerticalAlignment="Top"/>
            <TextBlock Name="txtOutput" HorizontalAlignment="Left" Height="193" Margin="19,141,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="393" Foreground="Blue"/>
            <TextBlock Name="txtStatusBar" HorizontalAlignment="Left" Height="20" Margin="19,386,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="393" Foreground="Red"/>
            <ComboBox Name="cmbServer" HorizontalAlignment="Left" Margin="125,76,0,0" VerticalAlignment="Top" Width="287" IsEditable="True"/>
        </Grid>
    </Window>





    • Edited by hetfield84 Friday, July 24, 2020 7:23 PM
    Friday, July 24, 2020 4:14 PM

Answers

  • $vCenter.add_DropdownClosed({
        $win.FindName('txtOutput').Text = 'vCenter chosen'
        write-host 'vCenter chosen'
    })


    \_(ツ)_/

    Saturday, July 25, 2020 3:33 AM

All replies

  • Please do not post colorized code in technical forums.

    Read the following instructions and edit your original post.


    \_(ツ)_/

    Friday, July 24, 2020 5:05 PM
  • Please format code cor4rectly.  Large numbers of blank lines make code unreadable.

    Without you XAML it is not possible to know how to answer your question.


    \_(ツ)_/

    Friday, July 24, 2020 7:16 PM
  • $vCenter.add_DropdownClosed({
        $win.FindName('txtOutput').Text = 'vCenter chosen'
        write-host 'vCenter chosen'
    })


    \_(ツ)_/

    Saturday, July 25, 2020 3:33 AM