如何删除组合框项目?我试过这段代码,但它不能工作。
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());
}
}}发布于 2011-07-27 00:00:26
不是尝试删除字符串,而是尝试:
cbRooms.Items.Remove((ComboBoxItem)item))发布于 2011-07-26 22:44:03
尝试删除ComboBoxItem,而不是:
(ComboBoxItem)item).Content.ToString()尝试:
(item)移除项后,可能还需要刷新组合框控件:
cbRooms.Items.Refresh();更新
你可以试试kzen在OP的评论中说的话。使用List<ComboBoxItem>存储您的项目,并在List上执行添加/删除操作。然后将列表绑定到您的ComboBox
cbRooms.ItemsSource = comboBoxItemList;然后,当您在List上执行操作时,调用刷新:
cbRooms.Items.Refresh();https://stackoverflow.com/questions/6831825
复制相似问题