private void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { CollectionChanged?.Invoke(this, e); if (this.parent == null || (e.Action == NotifyCollectionChangedAction.Remove && HasChildElements) || e.Action == NotifyCollectionChangedAction.Move || e.Action == NotifyCollectionChangedAction.Replace) { return; } bool isVisible = HasChildElements; SimpleCollectionView parent = this.parent; SimpleCollectionView child = this; if (isVisible) { var parents = new Stack <Tuple <SimpleCollectionView, SimpleCollectionView> >(); while (parent != null && !parent.filtered.ContainsKey(child.key)) { if (parent.Options.Filter != null && !parent.options.Filter(child.item)) { break; } parents.Push(new Tuple <SimpleCollectionView, SimpleCollectionView> (parent, child)); child = parent; parent = parent.parent; } while (parents.Count > 0) { var pair = parents.Pop(); pair.Item1.AddFiltered(pair.Item2.key); } } else { var parents = new List <Tuple <SimpleCollectionView, SimpleCollectionView> >(); while (parent != null && parent.filtered.ContainsKey(child.key) && !child.HasChildElements) { parents.Add(new Tuple <SimpleCollectionView, SimpleCollectionView> (parent, child)); child = parent; parent = parent.parent; } for (int i = 0; i < parents.Count; i++) { var pair = parents[i]; pair.Item1.RemoveFiltered(new[] { pair.Item2.key }); } } }
private void Reset(bool notify = true) { this.arranged.Clear(); this.filtered.Clear(); bool filtering = IsFiltering; foreach (var sourceItem in this.source.Cast <object>().Select(o => new { Item = o, Key = this.options.DisplaySelector(o) }).OrderBy(e => e.Key, Comparer)) { Element e = new Element { Item = sourceItem.Item }; this.arranged.Add(sourceItem.Key, e); SimpleCollectionView childView = null; IEnumerable children = (Options.ChildrenSelector != null) ? this.options.ChildrenSelector(sourceItem.Item) : sourceItem.Item as IEnumerable; if (Options.ChildOptions != null) { if (children == null) { throw new InvalidOperationException("ChildOptions specified, but element not enumerable or no selector"); } childView = new SimpleCollectionView(children, Options.ChildOptions, this, sourceItem.Key, sourceItem.Item); e.ChildrenView = childView; } if (!filtering || this.options.Filter(e.Item)) { this.filtered.Add(sourceItem.Key, e); } } if (notify) { OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } }
private SimpleCollectionView(IEnumerable source, SimpleCollectionViewOptions options, SimpleCollectionView parent = null, string key = null, object item = null) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } this.wasFilterNull = (options.Filter == null); this.source = source; this.parent = parent; this.item = item; this.key = key; SetOptions(options, notify: false); var changed = source as INotifyCollectionChanged; if (changed != null) { changed.CollectionChanged += OnSourceCollectionChanged; } }