I ran into this problem today using a 3rd party grid control who's
selected items I had bound to a ReactiveCollection. Unfortunately they
always call reset and then add for the selected rows rather than
remove/add. So I had to do create this:
/// <summary>
/// Extends reactive collections so that .ClearItems triggers remove
for the items.
/// </summary>
public class ReactiveCollectionEx<T> :
ReactiveUI.ReactiveCollection<T>
{
public ReactiveCollectionEx() : base()
{}
public ReactiveCollectionEx(IEnumerable<T> list)
: base(list)
{}
protected override void ClearItems()
{
for (int i = this.Count - 1; i >= 0; i--)
{
this.RemoveAt(i);
}
}
}
I appreciate it is inconsistent with ObservableCollection, but
sometimes needs must! The only other option would have been to extend
with the CollectionReset IObservable, but I was using this for
filtering, and the logic there likes Add/Remove (and given the small
number of rows selected performance is fine).
Dan
On May 25, 6:53 am, Paul Betts <
paul.be...@gmail.com> wrote:
> This is consistent with how ObservableCollection works, it also
> doesn't signal items removed when Reset() is called - I could consider
> adding in a "CollectionReset" IObservable though. Can you file this as
> a Github bug so I don't forget?
>
> One thing to keep in mind is, that you generally don't have to Dispose
> the IDisposable returned by Subscribe unless you explicitly want to
> terminate a subscription early. Objects that go out of scope and are
> GC'ed will disconnect themselves automagically.
>
> --
> Paul Betts <
p...@paulbetts.org>