user2631662 user2631662 - 1 year ago 209
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 Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download