// Parses the sub-properties of the given parent collection item and populates a corresponding // private list of ModelPropertyEntries that represents it. private void CreateCollection(ModelPropertyIndexer parentCollectionItem) { // Assert some assumptions that should be true at this point Fx.Assert(parentCollectionItem.ModelItem != null, "parentCollectionItem.ModelItem should not be null"); List <ModelProperty> subProperties = ExtensibilityAccessor.GetSubProperties(parentCollectionItem.ModelItem); if (subProperties == null || subProperties.Count < 1) { return; } // At this point we have at least one ModelProperty that acts as a subProperty of the // given ModelItem. Wrap the list in ModelPropertyEntries and exit. _properties = new List <ModelPropertyEntry>(subProperties.Count); for (int i = 0; i < subProperties.Count; i++) { _properties.Add(new ModelPropertyEntry(subProperties[i], (ModelPropertyValue)parentCollectionItem.PropertyValue)); } // Sort the sub-properties by their OrderToken as well as their name if (_properties != null) { _properties.Sort(); } }
// <summary> // Gets the underlying ModelItemCollection // </summary> // <returns>The underlying ModelItemCollection</returns> internal ModelItemCollection GetRawCollection() { ModelPropertyEntry parentAsEntry = ParentValue.ParentProperty as ModelPropertyEntry; if (parentAsEntry != null) { return(parentAsEntry.FirstModelProperty.Collection); } ModelPropertyIndexer parentAsIndexer = ParentValue.ParentProperty as ModelPropertyIndexer; if (parentAsIndexer != null) { ModelItemCollection modelItemCollection = parentAsIndexer.ModelItem as ModelItemCollection; // If the parent is an indexer, that means we are a collection within another collection // and the ModelItem of the indexer is really a ModelItemCollection. Fx.Assert(modelItemCollection != null, "modelItemCollection should not be null"); return(modelItemCollection); } Debug.Fail("A new class was introduced that derives from PropertyEntry. Need to update ModelPropertyValueCollection code as well."); return(null); }
// Updates internal structures, but does not fire any notification private PropertyValue InsertHelper(ModelItem item, int index) { // Only insert the value into the collection, if it's not already there. // Say that an ItemAdded event comes in even though this collection has not yet been used. // By requesting the instance of this collection for the first time, the collection // gets populated correctly and fully. Now when InsertExternal is called as a result // of the ItemAdded event, we would be adding the new item into the collection twice. // We need to prevent that from happening. if (_values.Count > index && object.Equals(_values[index].ModelItem, item)) { return(_values[index].PropertyValue); } ModelPropertyIndexer indexer = CreateModelPropertyIndexer(item, index); _values.Insert(index, indexer); // Adjust all indexes of the remaining indexers in the list for (int i = index + 1; i < _values.Count; i++) { _values[i].Index++; } return(indexer.PropertyValue); }
// <summary> // Swaps the items at the specified indexes // </summary> // <param name="currentIndex">Index of item 1</param> // <param name="newIndex">Index of item 2</param> public override void SetIndex(int currentIndex, int newIndex) { VerifyExistingIndex(currentIndex); VerifyExistingIndex(newIndex); if (currentIndex == newIndex) { return; } ModelItemCollection collection = this.GetRawCollection(); ModelPropertyIndexer currentIndexer = _values[currentIndex]; ModelPropertyIndexer newIndexer = _values[newIndex]; bool previouslyActive = SnoozeListeningToCollectionChanges(); try { // Remove the higher index first (doesn't affect the value of the lower index) if (currentIndex < newIndex) { collection.RemoveAt(newIndex); collection.RemoveAt(currentIndex); } else { collection.RemoveAt(currentIndex); collection.RemoveAt(newIndex); } // Insert the lower index first (fixes the value of the higher index) if (currentIndex < newIndex) { collection.Insert(currentIndex, newIndexer.ModelItem); collection.Insert(newIndex, currentIndexer.ModelItem); } else { collection.Insert(newIndex, currentIndexer.ModelItem); collection.Insert(currentIndex, newIndexer.ModelItem); } } finally { StartListeningToCollectionChanges(previouslyActive); } SetIndexExternal(currentIndex, newIndex); }
// Updates internal structures, but does not fire any notification private PropertyValue RemoveAtHelper(int index) { // invalidate the ModelPropertyEntryIndexer at the index and adjust all other indexes ModelPropertyIndexer indexer = _values[index]; DestroyModelPropertyIndexer(indexer); _values.RemoveAt(index); for (int i = index; i < _values.Count; i++) { _values[i].Index--; } return(indexer.PropertyValue); }
// Same as SetIndex, except it doesn't modify the raw collection, because it's // assumed that the raw collection was already modified externally. private void SetIndexExternal(int currentIndex, int newIndex) { if (currentIndex == newIndex) { return; } ModelPropertyIndexer currentIndexer = _values[currentIndex]; ModelPropertyIndexer newIndexer = _values[newIndex]; // Remove the higher index first (doesn't affect the value of the lower index) if (currentIndex < newIndex) { _values.RemoveAt(newIndex); _values.RemoveAt(currentIndex); } else { _values.RemoveAt(currentIndex); _values.RemoveAt(newIndex); } // Insert the lower index first (fixes the value of the higher index) if (currentIndex < newIndex) { _values.Insert(currentIndex, newIndexer); _values.Insert(newIndex, currentIndexer); } else { _values.Insert(newIndex, currentIndexer); _values.Insert(currentIndex, newIndexer); } newIndexer.Index = currentIndex; currentIndexer.Index = newIndex; // Fire OnChanged event this.OnCollectionChanged(new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Move, currentIndexer.PropertyValue, newIndex, currentIndex)); }
private static void DestroyModelPropertyIndexer(ModelPropertyIndexer indexer) { indexer.Index = -1; }
private ModelPropertyIndexer CreateModelPropertyIndexer(ModelItem item, int index) { ModelPropertyIndexer indexer = new ModelPropertyIndexer(item, index, this); return indexer; }
public CachedValues(ModelPropertyIndexer indexer) { _parent = indexer; }
// <summary> // Basic ctor // </summary> // <param name="parentProperty">Parent indexer</param> public ModelPropertyEntryCollection(ModelPropertyIndexer parentProperty) : base(parentProperty.PropertyValue) { CreateCollection(parentProperty); }
// Parses the sub-properties of the given parent collection item and populates a corresponding // private list of ModelPropertyEntries that represents it. private void CreateCollection(ModelPropertyIndexer parentCollectionItem) { // Assert some assumptions that should be true at this point Fx.Assert(parentCollectionItem.ModelItem != null, "parentCollectionItem.ModelItem should not be null"); List<ModelProperty> subProperties = ExtensibilityAccessor.GetSubProperties(parentCollectionItem.ModelItem); if (subProperties == null || subProperties.Count < 1) { return; } // At this point we have at least one ModelProperty that acts as a subProperty of the // given ModelItem. Wrap the list in ModelPropertyEntries and exit. _properties = new List<ModelPropertyEntry>(subProperties.Count); for (int i = 0; i < subProperties.Count; i++) { _properties.Add(new ModelPropertyEntry(subProperties[i], (ModelPropertyValue)parentCollectionItem.PropertyValue)); } // Sort the sub-properties by their OrderToken as well as their name if (_properties != null) { _properties.Sort(); } }
// <summary> // Basic ctor // </summary> // <param name="parentProperty">Parent indexer</param> public ModelPropertyEntryCollection(ModelPropertyIndexer parentProperty) : base(parentProperty.PropertyValue) { CreateCollection(parentProperty); }
public CachedValues(ModelPropertyIndexer indexer) { _parent = indexer; }
private static void DestroyModelPropertyIndexer(ModelPropertyIndexer indexer) { indexer.Index = -1; }
private ModelPropertyIndexer CreateModelPropertyIndexer(ModelItem item, int index) { ModelPropertyIndexer indexer = new ModelPropertyIndexer(item, index, this); return(indexer); }