Silverlight / WPF - How to resolve Operation is not valid while ItemsSource is in use. Access and modify eleme

Asked By Ramesh Nalla on 25-Feb-14 04:29 AM
Hi All,

I am getting the below error while adding a new item in WPF combo box. 

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.

please find the below code:
List<Empdetails> elist = iEI.GetEmployeeEmaiIds();
 if (cmbEmailIds.Items.Count == 0)
                {
    cmbEmailIds.Items.Clear();
                    cmbEmailIds.ItemsSource = elist;
                    cmbEmailIds.DisplayMemberPath = "EmailiId";
                    cmbEmailIds.SelectedValuePath = "EmailiId";
                }
                else
                {
                    
                    ComboBoxItem newItem=new ComboBoxItem();
                    newItem.Content=txtEmailId.Text.Trim();

                    cmbEmailIds.Items.Insert(cmbEmailIds.Items.Count, newItem);
                }

Any help is really appreciated.
Thanks 
GssRam
Erik Little replied to Ramesh Nalla on 28-Feb-14 02:35 PM
What it seems to look like the error is telling you is to not add the new item directly to the ComboBox because the ComboBox datasource is bound to the elist;

What you need to do is add the new item to the elist and it will show up in the ComboBox, because your ComboBox is bound to a datasource already.

Add the new item to your elist,  I'm not sure if you need to refresh or not but that is what the error is saying to you.