private void Archives_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (ignoreChanges) { return; } ignoreChanges = true; // If the collection was reset, then e.OldItems is empty. Just clear and reload. if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) { Archives.Clear(); foreach (var archive in index.Archives) { Archives.Add(new ArchiveViewModel(archive, this)); } } else { // Remove items from collection. var toRemove = new List <ArchiveViewModel>(); if (null != e.OldItems && e.OldItems.Count > 0) { foreach (var item in e.OldItems) { foreach (var existingItem in Archives) { if (existingItem.IsViewFor((Archive)item)) { toRemove.Add(existingItem); } } } } foreach (var item in toRemove) { Archives.Remove(item); } // Add new items to the collection. if (null != e.NewItems && e.NewItems.Count > 0) { foreach (var item in e.NewItems) { Archives.Add(new ArchiveViewModel((Archive)item, this)); } } } ignoreChanges = false; }
/// <summary> /// Reload the ArchiveManager from given directory (optional). /// </summary> /// <param name="indir"></param> public void Reload(DirectoryInfo indir = null) { if (indir != null) { _parentDirectoryInfo = indir; } var archives = _parentDirectoryInfo.GetFiles("*.archive").ToList(); Archives.Clear(); foreach (var fi in archives) { Archives.Add(fi.FullName, new Archive(fi.FullName)); } ReloadFiles(); }