How to delete combobox item? i tried this code but it does not work.
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
foreach (var item in cbRooms.Items)
{
if (((ComboBoxItem)item).Content.ToString() == cbRooms.Text.ToString())
{
cbRo开发者_StackOverflow中文版oms.Items.Remove(((ComboBoxItem)item).Content.ToString());
}
}}
Instead of trying to remove a string try:
cbRooms.Items.Remove((ComboBoxItem)item))
Try removing the ComboBoxItem rather than:
(ComboBoxItem)item).Content.ToString()
Try:
(item)
You may also need to refresh the combo box control after you remove the item:
cbRooms.Items.Refresh();
UPDATE
You could try what kzen said in the comments of the OP. Use a List<ComboBoxItem>
to store your items, and perform the add/remove operations on the List
. Then bind the list to your ComboBox
:
cbRooms.ItemsSource = comboBoxItemList;
Then when you do your operations on the List
call the refresh:
cbRooms.Items.Refresh();
精彩评论