user2631662 user2631662 - 1 month ago 21
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;
}

Answer

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;