Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Clear Selection in WPF ComboBox

Tags:

c#

wpf

I have a ComboBox in WPF whose ItemsSource is set to a list programmatically. How would I go about clearing the selection in an event handler? I've tried:

comboBox.SelectedIndex = -1;
comboBox.SelectedItem = null;
comboBox.SelectedValue = null;
comboBox.SelectedValue = "";

None of them have any effect.

like image 780
Bob Wintemberg Avatar asked Mar 09 '09 17:03

Bob Wintemberg


People also ask

Is there a clearselection method for WPF radcombobox?

The WPF RadComboBox object does not have a ClearSelection method. Could you tell me how to do this using the WPF control? Thanks for the clarification. You are right, there is no ClearSelection method for the WPF RadComboBox. Instead you need to implement and use the clear selection button as seen in this article.

How to remove selected items from combobox items?

Now if you click on the Delete button click, the selected item will be removed from the ComboBox items. The Foreground and Background attributes of ComboBoxItem represents the background and foreground colors of the item.

How do I clear the second combobox?

The easiest way to clear the second combobox is to just use Reset (). That will clear all the current selections and reset the combobox to its default selection. If I have answered your question, please mark your post as Solved.

What is combobox in WPF listing 12?

The ComboBox class in WPF represents a ComboBox control. The code snippet in Listing 12 creates a ComboBox at run-time and adds a few items to the ComboBox. Text property of ComboBox represents the text of the current selected item in a ComboBox. SelectedItem represents the first item in the currently selected items in a ComboBox.


4 Answers

comboBox.SelectedIndex = -1; works for me.

Are you doing anything else in your event handler? Are you using databinding?

like image 172
laktak Avatar answered Oct 18 '22 02:10

laktak


comboBox.SelectedIndex = -1;

Is the way to go. I don't know why it doesn't work for you; perhaps an event handler for SelectedIndexChanged changes the value?

like image 41
configurator Avatar answered Oct 18 '22 02:10

configurator


I found that I needed to also add:

comboBox.Text = "";

to get the text to clear

like image 4
Andrew Milford Avatar answered Oct 18 '22 01:10

Andrew Milford


I want to clear the ComboBox in DropDownClosed event of another ComboBox. Therefore I used following code inside of first ComboBox DropDownClosed event

private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
  this.comboBox.ItemsSource = null;
}
like image 1
Shashika Avatar answered Oct 18 '22 02:10

Shashika