How to deleselect / blank a databound ComboBox in Winforms/WPF?

How to deleselect / blank a databound ComboBox in Winforms/WPF?

In WinForms, you can deselect a databound ComboBox by setting its SelectedIndex property to -1, like this:

comboBox1.SelectedIndex = -1; 

In WPF, you can clear the selection of a databound ComboBox by setting its SelectedItem property to null, like this:

comboBox1.SelectedItem = null; 

Note that in both cases, the ComboBox must be databound to a data source, otherwise the SelectedIndex or SelectedItem property will not have any effect.

Examples

  1. "WinForms ComboBox Deselect Item"

    // Clear the selected index comboBox.SelectedIndex = -1; 

    Description: Sets the selected index to -1, effectively deselecting any item in the ComboBox.

  2. "WinForms ComboBox Clear Selection"

    // Clear the selected item comboBox.SelectedItem = null; 

    Description: Sets the selected item to null, removing the current selection in the ComboBox.

  3. "WinForms ComboBox Reset DataSource"

    // Reset the DataSource to null or a new empty list comboBox.DataSource = null; // OR comboBox.DataSource = new List<string>(); 

    Description: Clears the ComboBox's data source, effectively deselecting any item.

  4. "WinForms ComboBox Clear Items"

    // Clear the items in the ComboBox comboBox.Items.Clear(); 

    Description: Removes all items from the ComboBox, clearing the selection.

  5. "WinForms ComboBox Remove Binding"

    // Remove data binding comboBox.DataBindings.Clear(); 

    Description: Clears the data bindings on the ComboBox, which can help in deselecting the item.

WPF:

  1. "WPF ComboBox Deselect Item"

    // Set the selected index to -1 comboBox.SelectedIndex = -1; 

    Description: Sets the selected index to -1 in WPF, deselecting any item in the ComboBox.

  2. "WPF ComboBox Clear Selection"

    // Set the selected item to null comboBox.SelectedItem = null; 

    Description: Sets the selected item to null in WPF, clearing the current selection.

  3. "WPF ComboBox Reset ItemsSource"

    // Set the ItemsSource to null or an empty collection comboBox.ItemsSource = null; // OR comboBox.ItemsSource = new List<string>(); 

    Description: Clears the ComboBox's ItemsSource, effectively deselecting any item.

  4. "WPF ComboBox Clear Items"

    // Clear the items in the ComboBox comboBox.Items.Clear(); 

    Description: Removes all items from the WPF ComboBox, clearing the selection.

  5. "WPF ComboBox Remove Binding"

    // Remove data binding comboBox.SetBinding(ComboBox.ItemsSourceProperty, new Binding()); 

    Description: Sets a new empty binding on the ItemsSource property to clear data bindings, which can help in deselecting the item.


More Tags

kendo-listview offlineapps dart-http jtableheader storyboard interruption spark-submit call bootstrap-popover kubectl

More C# Questions

More Trees & Forestry Calculators

More Electrochemistry Calculators

More Transportation Calculators

More Organic chemistry Calculators