diff --git a/src/ObservableCollections/Internal/NotifyCollectionChangedSynchronizedView.cs b/src/ObservableCollections/Internal/NotifyCollectionChangedSynchronizedView.cs index d28aea7..6dc5974 100644 --- a/src/ObservableCollections/Internal/NotifyCollectionChangedSynchronizedView.cs +++ b/src/ObservableCollections/Internal/NotifyCollectionChangedSynchronizedView.cs @@ -1,8 +1,9 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; +using System.Linq; namespace ObservableCollections.Internal { @@ -25,7 +26,7 @@ public NotifyCollectionChangedSynchronizedView(ISynchronizedView paren parent.AttachFilter(this); } - public int Count => parent.Count; + public virtual int Count => parent.Count; public event NotifyCollectionChangedEventHandler? CollectionChanged; public event PropertyChangedEventHandler? PropertyChanged; @@ -152,7 +153,19 @@ public TView this[int index] { lock (view.SyncRoot) { - return view.list[index].Item2; + int currentIndex = 0; + foreach (var (value, itemView) in view.list) + { + if (view.CurrentFilter.IsMatch(value, itemView)) + { + if (currentIndex == index) + { + return itemView; + } + currentIndex++; + } + } + throw new ArgumentOutOfRangeException(nameof(index)); } } set => throw new NotSupportedException(); @@ -162,14 +175,40 @@ public TView this[int index] { get { - return this[index]; + lock (view.SyncRoot) + { + int currentIndex = 0; + foreach (var (value, itemView) in view.list) + { + if (view.CurrentFilter.IsMatch(value, itemView)) + { + if (currentIndex == index) + { + return itemView; + } + currentIndex++; + } + } + throw new ArgumentOutOfRangeException(nameof(index)); + } } set => throw new NotSupportedException(); } + public override int Count + { + get + { + lock (view.SyncRoot) + { + return view.list.Count(item => view.CurrentFilter.IsMatch(item.Item1, item.Item2)); + } + } + } + static bool IsCompatibleObject(object? value) { - return (value is T) || (value == null && default(T) == null); + return (value is T) || (value is TView) || (value == null && default(T) == null); } public bool IsReadOnly => true; @@ -199,11 +238,14 @@ public bool Contains(TView item) { lock (view.SyncRoot) { - foreach (var listItem in view.list) + foreach (var (value, itemView) in view.list) { - if (EqualityComparer.Default.Equals(listItem.Item2, item)) + if (view.CurrentFilter.IsMatch(value, itemView)) { - return true; + if (EqualityComparer.Default.Equals(itemView, item)) + { + return true; + } } } } @@ -234,13 +276,16 @@ public int IndexOf(TView item) lock (view.SyncRoot) { var index = 0; - foreach (var listItem in view.list) + foreach (var (value, itemView) in view.list) { - if (EqualityComparer.Default.Equals(listItem.Item2, item)) + if (view.CurrentFilter.IsMatch(value, itemView)) { - return index; + if (EqualityComparer.Default.Equals(itemView, item)) + { + return index; + } + index++; } - index++; } } return -1; @@ -280,4 +325,4 @@ public void RemoveAt(int index) throw new NotSupportedException(); } } -} \ No newline at end of file +}