Clear combo box in WPF

How to clear combo box in WPF? I tried this code:

 private void btClear1_Click(object sender, RoutedEventArgs e)
    {

        txtID.Text = String.Empty;
        this.cbType.SelectedItem = -1;
    }
+4
source share
4 answers

To clear the selected set, SelectedIndexnotSelectedItem

cboType.SelectedIndex = -1;

You can also set SelectedItemor SelectedValue, but change it to nullinstead of -1 (this indicates an object, not an integer).

cboType.SelectedItem = null;
+2
source

cbTypion.SelectedItem = -1to clear selection cbType.Items.Clear()to clear all items

+4
source

reset XAML.

, combobox XAML:

text={Binding viewmodelobject Name.property Name}

ViewModelPage:

viewmodelobject Name.property Name="";
+3


, , , , , cbType.Items.Clear(). , . XAML, , / .ItemSource. , .

.ItemSource ComboBox DataTable DefaultView, , cbType.Items.Clear(). , , .ItemSource, :

cbType.ItemsSource = null;

instead of this. Otherwise, if you try cbType.Items.Clear(), you will get:

Operation is not valid while ItemSource is in use. Access and modify 
elements with ItemsControl.ItemsSource instead


Clearing the selected item
I returned and saw the OP comment, stating that the desire was to clear the selection, not the box. For this, the remaining answers:

cbType.SelectedIndex = -1;  
cbType.Text = "";  // I've found the first line does this for me, as far as appearances, but found other postings saying it necessary - maybe they were using the property and it wasn't getting cleared, otherwise
+1
source

All Articles