user2631662 user2631662 - 4 months ago 69
C# Question

Clear a combobox in WPF

How do I clear a combobox in WPF? I have tried this code:

private void btClear1_Click(object sender, RoutedEventArgs e)

txtID.Text = String.Empty;
this.cbType.SelectedItem = -1;


To clear the selection set the SelectedIndex not the SelectedItem

cboType.SelectedIndex = -1;

You can set the SelectedItem or SelectedValue as well, but change it to null instead of -1 (these point to an object not an integer).

cboType.SelectedItem = null;