Home > Blockchain >  Reset WPF ComboBox to IsSelected value in Powershell
Reset WPF ComboBox to IsSelected value in Powershell

Time:12-21

Not too sure what I am missing here, but I have a few comboBoxes in a form and I would like to hit a "Clear" button and reset all the values to the original SelectedItem value.

xml:

<Window

  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="213" Width="513">

    <Grid>
        <ComboBox x:Name="cbox_Values" HorizontalAlignment="Left" Margin="247,77,0,0" VerticalAlignment="Top" Width="120">
            <ComboBoxItem IsSelected="True">Value1</ComboBoxItem>
            <ComboBoxItem>Value2</ComboBoxItem>
            <ComboBoxItem>Value3</ComboBoxItem>
        </ComboBox>
        <Button x:Name="btn_Clear" Content="Clear" HorizontalAlignment="Left" Margin="115,77,0,0" VerticalAlignment="Top" Width="86" Height="21" FontSize="11"/>
    </Grid>

</Window>

If I do it this way:

$wpf_btn_Clear.Add_Click({
    $wpf_cbox_Values.SelectedItem.Content = "Value1"
})

The current value gets wiped: enter image description here

If I do it this way:

$wpf_btn_Clear.Add_Click({
    $wpf_cbox_Values.SelectedItem.Content = "Value1"
    $wpf_cbox_Values.Items.Add("Value2")
    $wpf_cbox_Values.Items.Add("Value3")
})

I get doubles:

enter image description here

I am looking for the button to "reset" the comboBox with the original IsSelected item from the xml. For example, if i select Value3, then click the "Clear" button, the value goes back to Value1. This is in Powershell.

CodePudding user response:

$wpf_cbox_Values.SelectedIndex = 0 and/or $wpf_cbox_Values.SelectedItem = $wpf_cbox_Values.Items[0] should work.

CodePudding user response:

ComboBox.SelectedIndex Property

Perhaps try...

$wpf_btn_Clear.Add_Click({
     $wpf_cbox_Values.SelectedIndex = 0
})

Should keep your values, but set combobox index to zero which would be your first entry ("Value1").

  • Related