|
|
|
@ -13,6 +13,134 @@ namespace Perspex.Collections |
|
|
|
using System.ComponentModel; |
|
|
|
using System.Linq; |
|
|
|
|
|
|
|
public class PerspexReadOnlyListView<T> : IPerspexReadOnlyList<T>, IDisposable |
|
|
|
{ |
|
|
|
private IPerspexReadOnlyList<T> source; |
|
|
|
|
|
|
|
public PerspexReadOnlyListView() |
|
|
|
: this(null) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
public PerspexReadOnlyListView(IPerspexReadOnlyList<T> source) |
|
|
|
{ |
|
|
|
this.source = source; |
|
|
|
|
|
|
|
if (source != null) |
|
|
|
{ |
|
|
|
this.source.CollectionChanged += this.SourceCollectionChanged; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public event NotifyCollectionChangedEventHandler CollectionChanged; |
|
|
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged; |
|
|
|
|
|
|
|
public int Count |
|
|
|
{ |
|
|
|
get { return this.source?.Count ?? 0; } |
|
|
|
} |
|
|
|
|
|
|
|
public IPerspexReadOnlyList<T> Source |
|
|
|
{ |
|
|
|
get |
|
|
|
{ |
|
|
|
return this.source; |
|
|
|
} |
|
|
|
|
|
|
|
set |
|
|
|
{ |
|
|
|
if (this.source != null) |
|
|
|
{ |
|
|
|
this.source.CollectionChanged -= this.SourceCollectionChanged; |
|
|
|
|
|
|
|
if (this.CollectionChanged != null) |
|
|
|
{ |
|
|
|
var ev = new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Remove, |
|
|
|
this.source, |
|
|
|
0); |
|
|
|
this.CollectionChanged(this, ev); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
this.source = value; |
|
|
|
|
|
|
|
if (this.source != null) |
|
|
|
{ |
|
|
|
this.source.CollectionChanged += this.SourceCollectionChanged; |
|
|
|
|
|
|
|
if (this.CollectionChanged != null) |
|
|
|
{ |
|
|
|
var ev = new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Add, |
|
|
|
this.source.ToList(), |
|
|
|
0); |
|
|
|
this.CollectionChanged(this, ev); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public T this[int index] |
|
|
|
{ |
|
|
|
get { return this.source[index]; } |
|
|
|
} |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
this.source.CollectionChanged -= this.SourceCollectionChanged; |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator() |
|
|
|
{ |
|
|
|
return (this.source != null) ? |
|
|
|
this.source.GetEnumerator() : |
|
|
|
Enumerable.Empty<T>().GetEnumerator(); |
|
|
|
} |
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() |
|
|
|
{ |
|
|
|
return this.GetEnumerator(); |
|
|
|
} |
|
|
|
|
|
|
|
private void SourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) |
|
|
|
{ |
|
|
|
if (this.CollectionChanged != null) |
|
|
|
{ |
|
|
|
NotifyCollectionChangedEventArgs ev; |
|
|
|
|
|
|
|
switch (e.Action) |
|
|
|
{ |
|
|
|
case NotifyCollectionChangedAction.Add: |
|
|
|
ev = new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Add, |
|
|
|
e.NewItems, |
|
|
|
e.NewStartingIndex); |
|
|
|
break; |
|
|
|
case NotifyCollectionChangedAction.Remove: |
|
|
|
ev = new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Remove, |
|
|
|
e.OldItems, |
|
|
|
e.OldStartingIndex); |
|
|
|
break; |
|
|
|
case NotifyCollectionChangedAction.Replace: |
|
|
|
ev = new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Replace, |
|
|
|
e.NewItems, |
|
|
|
e.OldItems, |
|
|
|
e.OldStartingIndex); |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
throw new NotSupportedException("Action not yet implemented."); |
|
|
|
} |
|
|
|
|
|
|
|
this.CollectionChanged(this, ev); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class PerspexReadOnlyListView<TIn, TOut> : IPerspexReadOnlyList<TOut>, IDisposable |
|
|
|
{ |
|
|
|
private IPerspexReadOnlyList<TIn> source; |
|
|
|
|