Search code examples
c#wpfcombobox

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;
    }

Solution

  • 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;