Search code examples
wpfvisual-studiopowershellvariables

No result when clicking on ComboBox item in PowerShell


I'm creating a GUI from PS script and i'm having an issue with my ComboBox

<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="340,60,0,0" VerticalAlignment="Top" Width="64" Height="27">
                        <ComboBoxItem x:Name="Combo1" Content="DOMAIN1" HorizontalAlignment="Left" Width="63"/>
                        <ComboBoxItem x:Name="Combo2" Content="DOMAIN2" HorizontalAlignment="Left" Width="63"/>
                        <ComboBoxItem x:Name="Combo3" Content="DOMAIN3" HorizontalAlignment="Left" Width="63"/>
                    </ComboBox>

When I select DOMAIN 1 I see in my Textblock "This is DOMAIN1" but for DOMAIN2 and 3 this is not working. What am I doing wrong?

$var_comboBox.Add_SelectionChanged({

 if ($var_Combo1.Content -eq "DOMAIN1")
{ $var_result.Text = write "this is DOMAIN1"
}elseif
 ($var_Combo2.Content -eq "DOMAIN2" )
{ $var_result.Text = write "this is DOMAIN2"}

else{
($var_Combo3.Content -eq "DOMAIN3" )
 $var_result.Text = write "this is DOMAIN3"

}

It is only working for the first statement the other ones are not working..


Solution

  • The first if case is always true.

    You should check the value of the Content property of the SelectedItem:

    $var_comboBox.Add_SelectionChanged({
    
    $item = $var_comboBox.SelectedItem.Content
    
     if ($item -eq "DOMAIN1")
    { $var_result.Text = write "this is DOMAIN1"
    }elseif
     ($item -eq "DOMAIN2" )
    { $var_result.Text = write "this is DOMAIN2"}
    
    else{
    ($item -eq "DOMAIN3" )
     $var_result.Text = write "this is DOMAIN3"
    
    }