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.
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.
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.
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.
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.
comboBox.SelectedIndex = -1;
works for me.
Are you doing anything else in your event handler? Are you using databinding?
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?
I found that I needed to also add:
comboBox.Text = "";
to get the text to clear
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With