I have the problem: I can not add new item to the same combobox where clicked one of the comboboxItem. It returns me an InvalidOperationException: “Collection was modified; enumeration operation may not execute.” The only thing that I could find is this. I guess this must be the root of my problem, I still use the “comboboxlist” meanwhile I am changing it. But I can not reset the combobox or create a new one. (Although I managed to add new items to the combobox explicitly, f.e. with a button event.)
here is my xaml:
<ComboBox Name="comboBoxServers">
<ComboBoxItem Content="(local)"></ComboBoxItem>
<ComboBoxItem Content="<Browse for more...>" Selected="ComboBoxItem_Selected"></ComboBoxItem>
</ComboBox>
here is the event handler in c#
private void ComboBoxItem_Selected(object sender, RoutedEventArgs e)
{
comboBoxServers.Items.Add("test");
}
Any help would be greatly appreciated, cheers!
If you want to modify the
ComboBoxwhile handling the event you should set the event handler on theComboBoxand not on theComboBoxItem. You will then have to check whatComboBoxItemwas selected before proceeding.