private void AddCore(ModelItem key, ModelItem value, bool updateInstance) { try { this.EditInProgress = true; Fx.Assert(this.instance != null, "instance should not be null"); bool wasKeyInKeysOrValuesCollection = key != null && this.IsInKeysOrValuesCollection(key); bool wasValueInKeysOrValuesCollection = value != null && this.IsInKeysOrValuesCollection(value); if (updateInstance) { //no need to [....] if the ItemsCollection is not DictionaryItemsCollection wrapped by ModelItemCollectionImpl ModelItemCollectionImpl itemsCollectionImpl = this.ItemsCollectionModelItemCollection as ModelItemCollectionImpl; if (ItemsCollectionObject != null && itemsCollectionImpl != null) { try { ItemsCollectionObject.ShouldUpdateDictionary = false; object mutableKVPair = Activator.CreateInstance(this.ItemsCollectionKVPType, new object[] { key == null ? null : key.GetCurrentValue(), value != null ? value.GetCurrentValue() : null }); ModelItem mutableKVPairItem = this.modelTreeManager.WrapAsModelItem(mutableKVPair); itemsCollectionImpl.AddCore(mutableKVPairItem); } finally { ItemsCollectionObject.ShouldUpdateDictionary = true; } } this.instance.Add(key == null ? null : key.GetCurrentValue(), null != value ? value.GetCurrentValue() : null); } this.modelItems.Add(key, value); if (key != null && !wasKeyInKeysOrValuesCollection) { this.modelTreeManager.OnItemEdgeAdded(this, key); } if (value != null && !wasValueInKeysOrValuesCollection && value != key) { this.modelTreeManager.OnItemEdgeAdded(this, value); } if (null != this.CollectionChanged) { this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new KeyValuePair <ModelItem, ModelItem>(key, value))); } } finally { this.EditInProgress = false; } }
private void ClearCore(bool updateInstance) { try { this.EditInProgress = true; Fx.Assert(this.instance != null, "instance should not be null"); IList removed = this.modelItems.ToList <KeyValuePair <ModelItem, ModelItem> >(); if (updateInstance) { //no need to [....] if the ItemsCollection is not DictionaryItemsCollection wrapped by ModelItemCollectionImpl ModelItemCollectionImpl itemsCollectionImpl = this.ItemsCollectionModelItemCollection as ModelItemCollectionImpl; if (ItemsCollectionObject != null && itemsCollectionImpl != null) { try { ItemsCollectionObject.ShouldUpdateDictionary = false; itemsCollectionImpl.ClearCore(); } finally { ItemsCollectionObject.ShouldUpdateDictionary = true; } } this.instance.Clear(); } List <ModelItem> removedItems = new List <ModelItem>(this.modelItems.Keys.Concat(this.modelItems.Values).Distinct()); this.modelItems.Clear(); foreach (ModelItem item in removedItems.Distinct()) { if (item != null) { this.modelTreeManager.OnItemEdgeRemoved(this, item); } } if (null != this.CollectionChanged) { this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removed)); } } finally { this.EditInProgress = false; } }
private void RemoveCore(ModelItem key, bool updateInstance) { try { this.EditInProgress = true; Fx.Assert(this.instance != null, "instance should not be null"); ModelItem value = this.modelItems[key]; this.modelItems.Remove(key); if (key != null && !this.IsInKeysOrValuesCollection(key)) { this.modelTreeManager.OnItemEdgeRemoved(this, key); } if (value != null && !this.IsInKeysOrValuesCollection(value) && value != key) { this.modelTreeManager.OnItemEdgeRemoved(this, value); } if (updateInstance) { ModelItemCollectionImpl itemsCollectionImpl = ItemsCollectionModelItemCollection as ModelItemCollectionImpl; if (ItemsCollectionObject != null && itemsCollectionImpl != null) { try { ItemsCollectionObject.ShouldUpdateDictionary = false; ModelItem itemToBeRemoved = null; foreach (ModelItem item in itemsCollectionImpl) { ModelItem keyInCollection = item.Properties["Key"].Value; if (key == keyInCollection) { itemToBeRemoved = item; break; } if (key != null && keyInCollection != null) { object keyValue = key.GetCurrentValue(); // ValueType do not share ModelItem, a ModelItem is always created for a ValueType // ModelTreeManager always create a ModelItem even for the same string // So, we compare object instance instead of ModelItem for above cases. if (keyValue is ValueType || keyValue is string) { if (keyValue.Equals(keyInCollection.GetCurrentValue())) { itemToBeRemoved = item; break; } } } } if (itemToBeRemoved != null) { itemsCollectionImpl.RemoveCore(itemToBeRemoved); } } finally { ItemsCollectionObject.ShouldUpdateDictionary = true; } } this.instance.Remove(key == null ? null : key.GetCurrentValue()); } if (null != this.CollectionChanged) { this.CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, new KeyValuePair <ModelItem, ModelItem>(key, value))); } } finally { this.EditInProgress = false; } }