/// <summary> /// Adding molecules to or removing from the model also adds the Atoms and Bonds /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Molecules_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (Molecule m in e.NewItems) { foreach (Atom atom in m.Atoms) { if (!AllAtoms.Contains(atom)) { AllAtoms.Add(atom); } } foreach (Bond bond in m.Bonds) { if (!AllBonds.Contains(bond)) { AllBonds.Add(bond); } } } break; case NotifyCollectionChangedAction.Remove: foreach (Molecule m in e.OldItems) { foreach (Atom atom in m.Atoms.ToList()) { AllAtoms.Remove(atom); } foreach (Bond bond in m.Bonds.ToList()) { AllBonds.Remove(bond); } } break; } }
private void BondsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (Bond bond in e.NewItems) { bond.Parent = this; AllBonds.Add(bond); } break; case NotifyCollectionChangedAction.Move: break; case NotifyCollectionChangedAction.Remove: foreach (Bond bond in e.OldItems) { AllBonds.Remove(bond); bond.Parent = null; } break; case NotifyCollectionChangedAction.Replace: foreach (Bond bond in e.NewItems) { bond.Parent = this; AllBonds.Add(bond); } foreach (Bond bond in e.OldItems) { AllBonds.Remove(bond); bond.Parent = null; } break; case NotifyCollectionChangedAction.Reset: break; } }
private void AddNewAtomsAndBonds(NotifyCollectionChangedEventArgs e) { foreach (Molecule child in e.NewItems) { child.Parent = this; foreach (Atom atom in child.Atoms) { if (!AllAtoms.Contains(atom)) { AllAtoms.Add(atom); } } foreach (Bond bond in child.Bonds) { if (!AllBonds.Contains(bond)) { AllBonds.Add(bond); } } } }