|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Specialized; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Diagnostics.Contracts; |
| 6 | +using System.Reactive; |
| 7 | +using System.Reactive.Linq; |
| 8 | + |
| 9 | +namespace ReactiveUI.Winforms |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// IReactiveDerivedList represents a bindinglist whose contents will "follow" another |
| 13 | + /// collection; this method is useful for creating ViewModel collections |
| 14 | + /// that are automatically updated when the respective Model collection is updated. |
| 15 | + /// </summary> |
| 16 | + public interface IReactiveDerivedBindingList<T> : IReactiveDerivedList<T>, IBindingList {} |
| 17 | + |
| 18 | + class ReactiveDerivedBindingList<TSource, TValue> : |
| 19 | + ReactiveDerivedCollection<TSource, TValue>, IReactiveDerivedBindingList<TValue> |
| 20 | + { |
| 21 | + public ReactiveDerivedBindingList( |
| 22 | + IEnumerable<TSource> source, |
| 23 | + Func<TSource, TValue> selector, |
| 24 | + Func<TSource, bool> filter, |
| 25 | + Func<TValue, TValue, int> orderer, |
| 26 | + IObservable<Unit> signalReset) |
| 27 | + : base(source, selector, filter, orderer, signalReset) {} |
| 28 | + |
| 29 | + protected override void raiseCollectionChanged(NotifyCollectionChangedEventArgs e) |
| 30 | + { |
| 31 | + base.raiseCollectionChanged(e); |
| 32 | + if (this.ListChanged != null) { |
| 33 | + e.AsListChangedEventArgs().ForEach(x => this.ListChanged(this, x)); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + const string readonlyExceptionMessage = "Derived collections cannot be modified."; |
| 38 | + |
| 39 | + public object AddNew() |
| 40 | + { |
| 41 | + throw new NotSupportedException(readonlyExceptionMessage); |
| 42 | + } |
| 43 | + |
| 44 | + public void AddIndex(PropertyDescriptor property) |
| 45 | + { |
| 46 | + throw new NotSupportedException(); |
| 47 | + } |
| 48 | + |
| 49 | + public void ApplySort(PropertyDescriptor property, ListSortDirection direction) |
| 50 | + { |
| 51 | + throw new NotSupportedException(); |
| 52 | + } |
| 53 | + |
| 54 | + public int Find(PropertyDescriptor property, object key) |
| 55 | + { |
| 56 | + throw new NotSupportedException(); |
| 57 | + } |
| 58 | + |
| 59 | + public void RemoveIndex(PropertyDescriptor property) |
| 60 | + { |
| 61 | + throw new NotSupportedException(); |
| 62 | + } |
| 63 | + |
| 64 | + public void RemoveSort() |
| 65 | + { |
| 66 | + throw new NotSupportedException(); |
| 67 | + } |
| 68 | + |
| 69 | + public bool AllowNew { get { return false; } } |
| 70 | + |
| 71 | + public bool AllowEdit { get { return false; } } |
| 72 | + |
| 73 | + public bool AllowRemove { get { return false; } } |
| 74 | + |
| 75 | + public bool SupportsChangeNotification { get { return true; } } |
| 76 | + |
| 77 | + public bool SupportsSearching { get { return false; } } |
| 78 | + |
| 79 | + public bool SupportsSorting { get { return false; } } |
| 80 | + |
| 81 | + public bool IsSorted { get { return false; } } |
| 82 | + |
| 83 | + public PropertyDescriptor SortProperty { get { return null; } } |
| 84 | + |
| 85 | + public ListSortDirection SortDirection { get { return ListSortDirection.Ascending; } } |
| 86 | + |
| 87 | + public event ListChangedEventHandler ListChanged; |
| 88 | + } |
| 89 | + |
| 90 | + public static class ObservableCollectionMixin |
| 91 | + { |
| 92 | + /// <summary> |
| 93 | + /// Creates a collection whose contents will "follow" another |
| 94 | + /// collection; this method is useful for creating ViewModel collections |
| 95 | + /// that are automatically updated when the respective Model collection |
| 96 | + /// is updated. |
| 97 | + /// |
| 98 | + /// Note that even though this method attaches itself to any |
| 99 | + /// IEnumerable, it will only detect changes from objects implementing |
| 100 | + /// INotifyCollectionChanged (like ReactiveList). If your source |
| 101 | + /// collection doesn't implement this, signalReset is the way to signal |
| 102 | + /// the derived collection to reorder/refilter itself. |
| 103 | + /// </summary> |
| 104 | + /// <param name="selector">A Select function that will be run on each |
| 105 | + /// item.</param> |
| 106 | + /// <param name="filter">A filter to determine whether to exclude items |
| 107 | + /// in the derived collection.</param> |
| 108 | + /// <param name="orderer">A comparator method to determine the ordering of |
| 109 | + /// the resulting collection.</param> |
| 110 | + /// <param name="signalReset">When this Observable is signalled, |
| 111 | + /// the derived collection will be manually |
| 112 | + /// reordered/refiltered.</param> |
| 113 | + /// <returns>A new collection whose items are equivalent to |
| 114 | + /// Collection.Select().Where().OrderBy() and will mirror changes |
| 115 | + /// in the initial collection.</returns> |
| 116 | + public static IReactiveDerivedBindingList<TNew> CreateDerivedBindingList<T, TNew, TDontCare>( |
| 117 | + this IEnumerable<T> This, |
| 118 | + Func<T, TNew> selector, |
| 119 | + Func<T, bool> filter = null, |
| 120 | + Func<TNew, TNew, int> orderer = null, |
| 121 | + IObservable<TDontCare> signalReset = null) |
| 122 | + { |
| 123 | + Contract.Requires(selector != null); |
| 124 | + |
| 125 | + IObservable<Unit> reset = null; |
| 126 | + |
| 127 | + if (signalReset != null) { |
| 128 | + reset = signalReset.Select(_ => Unit.Default); |
| 129 | + } |
| 130 | + |
| 131 | + return new ReactiveDerivedBindingList<T, TNew>(This, selector, filter, orderer, reset); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Creates a collection whose contents will "follow" another |
| 136 | + /// collection; this method is useful for creating ViewModel collections |
| 137 | + /// that are automatically updated when the respective Model collection |
| 138 | + /// is updated. |
| 139 | + /// |
| 140 | + /// Be aware that this overload will result in a collection that *only* |
| 141 | + /// updates if the source implements INotifyCollectionChanged. If your |
| 142 | + /// list changes but isn't a ReactiveList/ObservableCollection, |
| 143 | + /// you probably want to use the other overload. |
| 144 | + /// </summary> |
| 145 | + /// <param name="selector">A Select function that will be run on each |
| 146 | + /// item.</param> |
| 147 | + /// <param name="filter">A filter to determine whether to exclude items |
| 148 | + /// in the derived collection.</param> |
| 149 | + /// <param name="orderer">A comparator method to determine the ordering of |
| 150 | + /// the resulting collection.</param> |
| 151 | + /// <returns>A new collection whose items are equivalent to |
| 152 | + /// Collection.Select().Where().OrderBy() and will mirror changes |
| 153 | + /// in the initial collection.</returns> |
| 154 | + public static IReactiveDerivedBindingList<TNew> CreateDerivedBindingList<T, TNew>( |
| 155 | + this IEnumerable<T> This, |
| 156 | + Func<T, TNew> selector, |
| 157 | + Func<T, bool> filter = null, |
| 158 | + Func<TNew, TNew, int> orderer = null) |
| 159 | + { |
| 160 | + return This.CreateDerivedBindingList(selector, filter, orderer, (IObservable<Unit>)null); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments