public static SynchronizationContextCollection <TResult> ToSyncedSynchronizationContextCollection <TSource, TResult>( this ISynchronizableNotifyChangedCollection <TSource> source, Func <TSource, TResult> converter, SynchronizationContext context) { lock (source.Synchronizer.LockObject) { var result = new SynchronizationContextCollection <TResult>(source.Select(converter), context); var collectionChangedListener = SynchronizableNotifyChangedCollectionHelper.CreateSynchronizableCollectionChangedEventListener(source, result, converter); result.Synchronizer.EventListeners.Add(collectionChangedListener); return(result); } }
public static FilteredObservableCollection <TResult> ToSyncedFilteredObservableCollection <TSource, TResult>( this ISynchronizableNotifyChangedCollection <TSource> source, Func <TSource, TResult> converter, Func <TResult, bool> filter) { var isDisposableType = typeof(IDisposable).GetTypeInfo().IsAssignableFrom(typeof(TResult).GetTypeInfo()); lock (source.Synchronizer.LockObject) { var result = new FilteredObservableCollection <TResult>(filter); foreach (var item in source) { result.Add(converter(item)); } var collectionChangedListener = SynchronizableNotifyChangedCollectionHelper.CreateSynchronizableCollectionChangedEventListener( source, result, converter, addAction: e => { var vm = converter((TSource)e.NewItems[0]); if (!filter(vm)) { return; } result.Insert(e.NewStartingIndex, vm); }, replaceAction: e => { if (isDisposableType) { ((IDisposable)result[e.NewStartingIndex]).Dispose(); } var replaceVm = converter((TSource)e.NewItems[0]); if (!filter(replaceVm)) { return; } result[e.NewStartingIndex] = replaceVm; }); result.Synchronizer.EventListeners.Add(collectionChangedListener); return(result); } }
public static SortedObservableCollection <TSource, TKey> ToSyncedSortedObservableCollection <TSource, TKey>( this ISynchronizableNotifyChangedCollection <TSource> source, Func <TSource, TKey> keySelector, IComparer <TKey> comparer = null, bool isDescending = false) { var isDisposableType = typeof(IDisposable).GetTypeInfo().IsAssignableFrom(typeof(TSource).GetTypeInfo()); lock (source.Synchronizer.LockObject) { var result = new SortedObservableCollection <TSource, TKey>(keySelector, comparer, isDescending); foreach (var item in source) { result.Add(item); } var collectionChangedListener = SynchronizableNotifyChangedCollectionHelper.CreateSynchronizableCollectionChangedEventListener(source, result, _ => _, removeAction: e => { var removeSourceItem = (TSource)e.OldItems[0]; result.RemoveAt(result.IndexOf(removeSourceItem)); if (isDisposableType) { ((IDisposable)removeSourceItem).Dispose(); } }, moveAction: e => { }, replaceAction: e => { var removeSourceItem = (TSource)e.OldItems[0]; result.RemoveAt(result.IndexOf(removeSourceItem)); result.Add((TSource)e.NewItems[0]); if (isDisposableType) { ((IDisposable)removeSourceItem).Dispose(); } } ); result.Synchronizer.EventListeners.Add(collectionChangedListener); return(result); } }
public static ObservableSynchronizedCollection <TResult> ToSyncedObservableSynchronizedCollection <TSource, TResult>( this ISynchronizableNotifyChangedCollection <TSource> source, Func <TSource, TResult> converter) { lock (source.Synchronizer.LockObject) { var result = new ObservableSynchronizedCollection <TResult>(); foreach (var item in source) { result.Add(converter(item)); } var collectionChangedListener = SynchronizableNotifyChangedCollectionHelper.CreateSynchronizableCollectionChangedEventListener( source, result, converter); result.Synchronizer.EventListeners.Add(collectionChangedListener); return(result); } }