开发者

Remove combobox item from combobox WPF

开发者 https://www.devze.com 2023-03-23 04:47 出处:网络
How to delete combobox item? i tried this code but it does not work. private void btnAdd_Click(object sender, RoutedEventArgs e)

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消