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())
{
cbRooms.Items.Remove(((ComboBoxItem)item).Content.ToString());
}
}}
与其尝试删除字符串,不如尝试:
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.