public Capture(ListBox listBox) { this.listBox = listBox; incc = listBox.ItemsSource as INotifyCollectionChanged; incc.CollectionChanged += new NotifyCollectionChangedEventHandler(incc_CollectionChanged); }
protected override SnapshotState CheckValueChanged(object newValue) { if (_listenedCollectionAsINotifyCollectionChanged != null && ReferenceEquals(_listenedCollectionAsINotifyCollectionChanged, newValue)) { return this.State; } ReleaseListeners(); var newValueAsINotifyCollectionChanged = newValue as INotifyCollectionChanged; var newValueAsIEnumerable = newValue as IEnumerable; if (newValueAsINotifyCollectionChanged == null || newValueAsIEnumerable == null || newValue.GetType() != this.SnapshotValue.GetType()) { return SnapshotState.CHANGED; } _listenedCollectionAsINotifyCollectionChanged = newValueAsINotifyCollectionChanged; _listenedCollectionAsIEnumerable = newValueAsIEnumerable; _listenedCollectionAsINotifyCollectionChanged.CollectionChanged += OnCollectionChanged; return CheckCollectionChanged(); }
public CollectionObserver(INotifyCollectionChanged collection, Action<NotifyCollectionChangedEventArgs> collectionChanged) { _collectionChanged = collectionChanged; _collectionObserver = Observable.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>( ev => collection.CollectionChanged += ev, ev => collection.CollectionChanged -= ev) .Subscribe(e => CollectionChanged(e.EventArgs)); }
/// <summary> /// Remove a handler for the given source's event. /// </summary> public static void RemoveHandler(INotifyCollectionChanged source, EventHandler<NotifyCollectionChangedEventArgs> handler) { if (handler == null) throw new ArgumentNullException("handler"); CurrentManager.ProtectedRemoveHandler(source, handler); }
public CollectionNotificationChainManager(INotifyCollectionChanged notifyingCollection) : this() { notifyingCollection.ThrowIfNull ("notifyingCollection"); ObserveCollection (notifyingCollection); }
/// <summary> /// Initializes a new instance of the <see cref="CollectionObserver"/> class. /// </summary> /// <param name="collection">The collection.</param> /// <param name="tag">The tag.</param> /// <param name="mementoService">The memento service.</param> /// <exception cref="ArgumentNullException">The <paramref name="collection"/> is <c>null</c>.</exception> public CollectionObserver(INotifyCollectionChanged collection, object tag = null, IMementoService mementoService = null) : base(tag, mementoService) { Argument.IsNotNull("collection", collection); _weakEventListener = this.SubscribeToWeakCollectionChangedEvent(collection, OnCollectionChanged); }
/// <summary> /// Constructor with collection and property name</summary> /// <param name="collection">Collection whose changes are listened to</param> /// <param name="propertyName">Property whose value change is listened to</param> public CollectionChangeListener(INotifyCollectionChanged collection, string propertyName) { m_value = collection; PropertyName = propertyName; Subscribe(); }
/// <summary> /// Begins observing the given notifying collection. /// </summary> /// <param name="notifyingCollection"></param> public void ObserveCollection(INotifyCollectionChanged notifyingCollection) { notifyingCollection.ThrowIfNull ("notifyingCollection"); if (IsDisposed) return; if (myObservedCollections.ContainsKey (notifyingCollection)) return; myObservedCollections[notifyingCollection] = () => notifyingCollection.CollectionChanged -= myCollectionChangedEventHandler; NotificationChainPropertyAttribute.CallProperties (notifyingCollection); notifyingCollection.CollectionChanged += myCollectionChangedEventHandler; var enumerable = notifyingCollection as IEnumerable; if (enumerable != null) { foreach (var item in enumerable) { var inpc = item as INotifyPropertyChanged; if (inpc != null) base.Observe (inpc); } } }
private void DetachNotificationEvents(INotifyCollectionChanged notifyCollection) { if (notifyCollection != null) { notifyCollection.CollectionChanged -= OnCollectionChanged; } }
public StationConnectionChecker(IStationFeedService stationFeedService, IConfigurationService configurationService) { ConfigurationService = configurationService; stationCollection = stationFeedService.CachedStations; stationCollection.CollectionChanged += StationCollection_OnCollectionChanged; }
public CollectionChangedEventListener(INotifyCollectionChanged source, NotifyCollectionChangedEventHandler handler) { if (source == null) { throw new ArgumentNullException("source"); } if (handler == null) { throw new ArgumentNullException("handler"); } this.source = source; this.handler = handler; }
private UICollectionSynchronizer(IList<CardViewModel> collection, INotifyCollectionChanged collectionChanged, StackLayout stackLayout, GamePageModel gamePageModel) { this.stackLayout = stackLayout; this.gamePageModel = gamePageModel; ((INotifyCollectionChanged)collection).CollectionChanged += (object sender, NotifyCollectionChangedEventArgs e) => { if (e.Action == NotifyCollectionChangedAction.Reset) { this.stackLayout.Children.Clear (); this.mapper.Clear (); } else { if(e.OldItems != null) { foreach (CardViewModel card in e.OldItems) { this.RemoveItem(card); } } if(e.NewItems != null) { foreach (CardViewModel card in e.NewItems) { this.AddItem(card); } } } }; foreach (CardViewModel card in collection) { this.AddItem (card); } }
public CollectionSynchronizer(INotifyCollectionChanged source, ObservableCollection<object> target, IValueConverter valueConverter) { _source = source; _target = target; _valueConverter = valueConverter; _source.CollectionChanged += OnSourceCollectionChanged; }
public void Include(INotifyCollectionChanged changed) { changed.CollectionChanged += (s, e) => { var test = string.Format("{0}{1}{2}{3}{4}", e.Action, e.NewItems, e.NewStartingIndex, e.OldItems, e.OldStartingIndex); }; }
/// <summary> /// Initializes a new instance of the <see cref="WeakCollectionChangedListener"/> /// </summary> /// <param name="source">The source event source</param> /// <param name="listener">The listener event listener</param> private WeakCollectionChangedListener( INotifyCollectionChanged source, ICollectionChangedListener listener) { this._source = source; this._source.CollectionChanged += this.SourceCollectionChanged; this._weakListener = new WeakReference(listener); }
/// <summary> /// Initializes a new instance of the <see cref="CollectionObserver"/> class. /// </summary> /// <param name="collection">The collection.</param> /// <param name="tag">The tag.</param> /// <param name="mementoService">The memento service.</param> /// <exception cref="ArgumentNullException">The <paramref name="collection"/> is <c>null</c>.</exception> public CollectionObserver(INotifyCollectionChanged collection, object tag = null, IMementoService mementoService = null) : base(tag, mementoService) { Argument.IsNotNull("collection", collection); _collection = collection; _collection.CollectionChanged += OnCollectionChanged; }
public static void UnhookCollectionChangedEvent(INotifyCollectionChanged collection, object target, PropertyInfo property) { _collectionPropertyMap .Where(t => t.CollectionReference.Target == collection && t.TargetReference.Target == target && t.Property == property) .ToList().ForEach(t => _collectionPropertyMap.Remove(t)); collection.CollectionChanged -= HandleCollectionChanged; }
// // Public Methods // /// <summary> /// Add a listener to the given source's event. /// </summary> public static void AddListener(INotifyCollectionChanged source, IWeakEventListener listener) { if (source == null) throw new ArgumentNullException("source"); if (listener == null) throw new ArgumentNullException("listener"); CurrentManager.ProtectedAddListener(source, listener); }
private static NotifyCollectionChangedEventHandler SetupCollectionChangedEventHandler(DependencyObject dependencyObject, INotifyCollectionChanged observableCollection) { NotifyCollectionChangedEventHandler sourceChangedHandler = (s, ev) => SelectedItemsCollectionChanged(dependencyObject, ev); observableCollection.CollectionChanged += sourceChangedHandler; return sourceChangedHandler; }
/// <summary> /// Hookups the handler to the collection changed event. When the value changes it removes the handler. /// </summary> /// <param name="oldValue">The old collection.</param> /// <param name="newValue">The new collection.</param> /// <param name="handler">The handler to hookup.</param> public static void HookupToCollectionChanged(INotifyCollectionChanged oldValue, INotifyCollectionChanged newValue, NotifyCollectionChangedEventHandler handler) { if (oldValue != null) oldValue.CollectionChanged -= handler; if (newValue != null) newValue.CollectionChanged += handler; }
public Capture(ListView ListView) { this.ListView = ListView; this.incc = ListView.ItemsSource as INotifyCollectionChanged; if (this.incc != null) { this.incc.CollectionChanged += this.incc_CollectionChanged; } }
/// <summary> /// Disconnects this listener from the collection change event /// </summary> internal void Disconnect() { if (this._source != null) { this._source.CollectionChanged -= this.SourceCollectionChanged; this._source = null; this._weakListener = null; } }
void LegendItemsCollectionChangedHandler(INotifyCollectionChanged notifyCollectionChanged, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) { switch (notifyCollectionChangedEventArgs.Action) { case NotifyCollectionChangedAction.Add: foreach (LegendItemViewModel newLegendItem in notifyCollectionChangedEventArgs.NewItems) if (LegendItems.All(l => l.SeriesName != newLegendItem.SeriesName)) LegendItems.Add(newLegendItem); break; } }
internal void Disconnect() { if (this.source == null) { return; } this.source.CollectionChanged -= new NotifyCollectionChangedEventHandler(this.SourceCollectionChanged); this.source = null; this.weakListener = null; }
public static NotifyCollectionChangedEventHandler SetItemsCollection(this MapLayer layer, INotifyCollectionChanged collection, DataTemplate template) { if (collection == null) return null; Action<IList> RemoveMapOverlays = (items) => { if (items == null || items.Count == 0) return; for (int i = layer.Count - 1; i >= 0; i--) if (items.Contains(layer[i].Content)) layer.RemoveAt(i); }; Action<IList> CreateMapOverlays = (items) => { if (items == null || items.Count == 0) return; foreach (var item in items) { var overlay = new MapOverlay(); var coord = item as IHasCoordinate; overlay.ContentTemplate = template; overlay.Content = item; overlay.GeoCoordinate = coord.Coordinate; overlay.PositionOrigin = new System.Windows.Point(0.5, 1); layer.Add(overlay); } }; NotifyCollectionChangedEventHandler ItemsChanged = (sender, e) => { switch (e.Action) { case System.Collections.Specialized.NotifyCollectionChangedAction.Add: CreateMapOverlays(e.NewItems); break; case System.Collections.Specialized.NotifyCollectionChangedAction.Move: break; case System.Collections.Specialized.NotifyCollectionChangedAction.Remove: RemoveMapOverlays(e.OldItems); break; case System.Collections.Specialized.NotifyCollectionChangedAction.Replace: RemoveMapOverlays(e.OldItems); CreateMapOverlays(e.NewItems); break; case System.Collections.Specialized.NotifyCollectionChangedAction.Reset: layer.Clear(); CreateMapOverlays(collection as IList); break; default: break; } }; collection.CollectionChanged += ItemsChanged; return ItemsChanged; }
/// <summary> /// Adds a weak event listener for a CollectionChanged event. /// </summary> /// <param name="source">The source of the event.</param> /// <param name="handler">The event handler.</param> /// <exception cref="ArgumentNullException">source must not be <c>null</c>.</exception> /// <exception cref="ArgumentNullException">handler must not be <c>null</c>.</exception> protected void AddWeakEventListener(INotifyCollectionChanged source, NotifyCollectionChangedEventHandler handler) { if (source == null) { throw new ArgumentNullException("source"); } if (handler == null) { throw new ArgumentNullException("handler"); } CollectionChangedEventListener listener = new CollectionChangedEventListener(source, handler); collectionChangedListeners.Add(listener); CollectionChangedEventManager.AddListener(source, listener); }
/// <summary> /// Remove a listener to the given source's event. /// </summary> public static void RemoveListener(INotifyCollectionChanged source, IWeakEventListener listener) { /* for app-compat, allow RemoveListener(null, x) - it's a no-op (see Dev10 796788) if (source == null) throw new ArgumentNullException("source"); */ if (listener == null) throw new ArgumentNullException("listener"); CurrentManager.ProtectedRemoveListener(source, listener); }
private static void OnSelectedItemsChanged(INotifyCollectionChanged collection, IList oldItems, IList newItems) { isSyncingSelection = true; var gridViews = GetOrCreateGridViews(collection); foreach (var gridView in gridViews) { SyncSelection(gridView, oldItems, newItems); } isSyncingSelection = false; }
private static void OnSelectedItemsChanged(INotifyCollectionChanged collection, IList oldItems, IList newItems) { isSyncingSelection = true; var behaviors = GetOrCreateSelectionBehaviors(collection); foreach (var behavior in behaviors) { SyncSelection(behavior, oldItems, newItems); } isSyncingSelection = false; }
private static void SetupOnUnloadedHandler(DependencyObject dependencyObject, INotifyCollectionChanged observableCollection, NotifyCollectionChangedEventHandler sourceChangedHandler) { RoutedEventHandler unloadedEventHandler = null; unloadedEventHandler = (sender, args) => { observableCollection.CollectionChanged -= sourceChangedHandler; ((ListViewBase)sender).SelectionChanged -= OnGridSelectionChanged; ((ListViewBase)sender).Unloaded -= unloadedEventHandler; }; ((ListViewBase)dependencyObject).Unloaded += unloadedEventHandler; }
/// <summary> /// Removes a previously added listener from the provided source. /// </summary> /// <param name = "source">The source.</param> /// <param name = "listener">The listener.</param> public static void RemoveListener(INotifyCollectionChanged source, IWeakEventListener listener) { CurrentManager.ProtectedRemoveListener(source, listener); }
/// <summary> /// Initializes a new instance of the <see cref="ReactiveTableViewSource{TSource}"/> class. /// </summary> /// <param name="tableView">The table view.</param> /// <param name="collection">The collection.</param> /// <param name="cellKey">The cell key.</param> /// <param name="sizeHint">The size hint.</param> /// <param name="initializeCellAction">The initialize cell action.</param> public ReactiveTableViewSource(UITableView tableView, INotifyCollectionChanged collection, NSString cellKey, float sizeHint, Action <UITableViewCell> initializeCellAction = null) : this(tableView) { Data = new[] { new TableSectionInformation <TSource, UITableViewCell>(collection, cellKey, sizeHint, initializeCellAction) }; }
/// <summary> /// Binds the property on many elements into a combination for which <paramref name="handler"/> is invoked whenever its value changes. /// This is useful for instance when you have a property on a collection that is the reduction of a property of collection element (in which case you can use the handler to set the reduced value). /// </summary> /// <typeparam name="TElement"> The type of the elements of the specified observable collection. </typeparam> /// <typeparam name="TElementProperty"> The type of the property on <typeparamref name="TElement"/> whose name is <paramref name="propertyName"/> and whose values are to be reduced. </typeparam> /// <param name="collection"> The collection to monitor for property changes. </param> /// <param name="propertyName"> The name of the property on the collection elements to monitor. </param> /// <param name="handler"> The function handling the change of the reduced value. The first argument is the collection and the second the new value. </param> /// <param name="combine"> The function taking the previous reduced value and an updated value of one of the properies, and returns their reduction. </param> /// <param name="valueIfCollectionEmpty"> The value to be taken if the specified collection is empty. </param> /// <param name="equalityComparer"> The function comparing <typeparamref name="TElementProperty"/>s for equality, determining when to invoke <paramref name="handler"/>. </param> public static void BindCollective <TElement, TElementProperty>(this INotifyCollectionChanged collection, string propertyName, PropertyChangedEventHandler handler, Func <TElementProperty /*previous resultant*/, TElementProperty /*new property*/, TElementProperty> combine, Option <TElementProperty> valueIfCollectionEmpty = default, IEqualityComparer <TElementProperty> equalityComparer = null) { Contract.Requires(collection != null); Contract.Requires(collection is IEnumerable <TElement>); Contract.Requires(HasProperty <TElement>(propertyName)); Contract.Requires(handler != null); Contract.Requires(combine != null); equalityComparer = equalityComparer ?? EqualityComparer <TElementProperty> .Default; Option <TElementProperty> resultant = Option <TElementProperty> .None; collection.Bind(propertyName, elementPropertyChanged, typeof(TElement), true, onElementRemoved); setResultant(recomputeResultant()); // if handler shouldn't be triggered on the initially present elements, set as resultant = recomputeResultant() rather than through setResultant Option <TElementProperty> recomputeResultant() { Option <TElementProperty> result = Option <TElementProperty> .None; foreach (var element in (IEnumerable <TElement>)collection) { var propValue = GetPropertyValue <TElement, TElementProperty>(element, propertyName); if (!result.HasValue) { result = propValue; } else { result = combine(result.Value, propValue); } } return(result); // returns Option.None when the collection is empty } void onElementRemoved(object sender, PropertyChangedEventArgs e) { Contract.Assert(resultant.HasValue); var propValue = GetPropertyValue <TElement, TElementProperty>((TElement)sender, e.PropertyName); if (equalityComparer.Equals(propValue, resultant.Value)) // if the resultant value was removed from the collection { setResultant(recomputeResultant()); } } void elementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == propertyName) { var newValue = GetPropertyValue <TElement, TElementProperty>((TElement)sender, e.PropertyName); var newResultant = resultant.HasValue ? combine(resultant.Value, newValue) : newValue; setResultant(newResultant); } } void setResultant(Option <TElementProperty> newResultant) { // newResultant: None means the collection is empty. TElementProperty newValue; if (newResultant.HasValue) { newValue = newResultant.Value; } else { if (valueIfCollectionEmpty.HasValue) { newValue = valueIfCollectionEmpty.Value; } else { // don't trigger the handler if there is no value to set return; } } if (resultant.HasValue) { TElementProperty oldValue = resultant.Value; if (!equalityComparer.Equals(oldValue, newValue)) { handle(oldValue, newValue); } } else { TElementProperty oldValue = valueIfCollectionEmpty.ValueOrDefault(); handle(oldValue, newValue); } } void handle(TElementProperty oldValue, TElementProperty newValue) { var arg = new PropertyMutatedEventArgs <TElementProperty>(propertyName, oldValue, newValue); handler(collection, arg); } }
public static CollectionProcessing <TSourceItem, TReturnValue> CollectionItemsProcessing <TSourceItem, TReturnValue>(this INotifyCollectionChanged source, Func <TSourceItem[], ICollectionComputing, TReturnValue[]> newItemsProcessor = null, Action <TSourceItem[], ICollectionComputing, TReturnValue[]> oldItemsProcessor = null, Action <TSourceItem, ICollectionComputing, TReturnValue> moveItemProcessor = null) { return(new CollectionProcessing <TSourceItem, TReturnValue>( source: source, newItemsProcessor: newItemsProcessor, oldItemsProcessor: oldItemsProcessor, moveItemProcessor: moveItemProcessor)); }
private static INotifyCollectionChanged getSource( INotifyCollectionChanged source, Expression <Func <TSourceItem, INotifyCollectionChanged> > selectorExpression) { return(source.Selecting(selectorExpression)); }
public static MvxNotifyCollectionChangedEventSubscription WeakSubscribe(this INotifyCollectionChanged source, EventHandler <NotifyCollectionChangedEventArgs> eventHandler) { return(new MvxNotifyCollectionChangedEventSubscription(source, eventHandler)); }
/// <summary> /// Registers a collection to the previous and next states of this operation. /// </summary> /// <param name="scope"></param> /// <param name="collection">The collection to register.</param> public void RegisterCollection([NotNull] SelectionScope scope, [NotNull] INotifyCollectionChanged collection) { previousState.RegisterCollection(scope, collection); nextState.RegisterCollection(scope, collection); }
public void Include(INotifyCollectionChanged changed) { changed.CollectionChanged += (s, e) => { var test = $"{e.Action}{e.NewItems}{e.NewStartingIndex}{e.OldItems}{e.OldStartingIndex}"; }; }
public CollectionChangedTrigger(INotifyCollectionChanged source, IScheduler tripScheduler, Logger logger) : this(source, -1, tripScheduler, logger) { }
private static void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { INotifyCollectionChanged collection = (INotifyCollectionChanged)sender; OnSelectedItemsChanged(collection, args.OldItems, args.NewItems); }
public static void SetSelectedItems(DependencyObject obj, INotifyCollectionChanged value) { obj.SetValue(SelectedItemsProperty, value); }
/* ----------------------------------------------------------------- */ /// /// ToBindingSource /// /// <summary> /// INotifyCollectionChanged を実装するコレクションを /// BindingSource オブジェクトに変換します。 /// </summary> /// /// <remarks> /// CollectionChanged イベント発生時に ResetBindings(false) を /// 実行します。 /// </remarks> /// /// <param name="src">コレクションオブジェクト</param> /// /* ----------------------------------------------------------------- */ public static BindingSource ToBindingSource(this INotifyCollectionChanged src) => ToBindingSource(src, SynchronizationContext.Current);
public static ObservableVector <T> ToObservableVector <T>(this INotifyCollectionChanged s) { return(new ObservableVector <T>(s)); }
public LastComputing( INotifyCollectionChanged source, TSourceItem defaultValue = default(TSourceItem)) : base(source, getIndex(source), defaultValue) { }
public ObservableCollectionWrapper(ICollection collection, INotifyCollectionChanged notifier) { _collection = collection; notifier.CollectionChanged += (sender, args) => OnCollectionChanged(args); }
public Extending( INotifyCollectionChanged source) : base(Utils.getCapacity(source)) { _source = source; _thisAsSourceCollectionChangeProcessor = this; }
private void ConnectToCollectionChangedOnScheduler(INotifyCollectionChanged firstCollection, INotifyCollectionChanged secondCollection, IScheduler notificationScheduler) { firstCollection.CollectionChanged += (_, args) => notificationScheduler.Schedule(Unit.Default, (__, ___) => OnFirstCollectionChanged(args)); secondCollection.CollectionChanged += (_, args) => notificationScheduler.Schedule(Unit.Default, (__, ___) => OnSecondCollectionChanged(args)); }
/// <summary> /// Initializes a new instance of <see cref="CollectionActiveAwareBehavior"/>. /// </summary> /// <param name="collection">The collection to monitor.</param> /// <remarks>This instance will keep a <see cref="WeakReference"/> to the /// <paramref name="collection"/>, so the collection can be garbage collected.</remarks> public CollectionActiveAwareBehavior(INotifyCollectionChanged collection) { _collection = new WeakReference(collection); }
public static CollectionDisposing <TSourceItem> CollectionDisposing <TSourceItem>(this INotifyCollectionChanged source) where TSourceItem : IDisposable { return(new CollectionDisposing <TSourceItem>( source: source)); }
/// <summary> /// Registers a weak handler to <see cref="INotifyCollectionChanged.CollectionChanged"/>. /// </summary> /// <typeparam name="TSubscriber">The type of the event subscriber.</typeparam> /// <param name="source">The event source.</param> /// <param name="subscriber">The event subscriber.</param> /// <param name="weakHandler">The weak handler.</param> /// <returns>A registration object that can be used to deregister from the event.</returns> public static IDisposable RegisterCollectionChangedWeak <TSubscriber>(this INotifyCollectionChanged source, TSubscriber subscriber, [EmptyCapture] Action <TSubscriber, object, NotifyCollectionChangedEventArgs> weakHandler) where TSubscriber : class { return(new WeakNotifyCollectionChangedHandler <TSubscriber>(source, subscriber, weakHandler)); }
private void ConnectToCollectionChanged(INotifyCollectionChanged firstCollection, INotifyCollectionChanged secondCollection) { firstCollection.CollectionChanged += (_, args) => OnFirstCollectionChanged(args); secondCollection.CollectionChanged += (_, args) => OnSecondCollectionChanged(args); }
/// <summary> /// Rebuilds the list if a collection changes. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">Information about the event.</param> private void ChildCollectionsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { Debug.Assert(e.Action != NotifyCollectionChangedAction.Reset, "Reset is not supported."); if (e.Action == NotifyCollectionChangedAction.Add) { e.NewItems .OfType <IList>() .ForEachWithIndex((newCollection, index) => { int startingIndex = GetStartingIndexOfCollectionAtIndex(e.NewStartingIndex + index); foreach (T item in newCollection.OfType <T>().Reverse()) { this.Mutate(items => items.Insert(startingIndex, item)); } INotifyCollectionChanged notifyCollectionChanged = newCollection as INotifyCollectionChanged; if (notifyCollectionChanged != null) { notifyCollectionChanged.CollectionChanged += ChildCollectionCollectionChanged; } }); } else if (e.Action == NotifyCollectionChangedAction.Remove) { foreach (IList oldCollection in e.OldItems) { INotifyCollectionChanged notifyCollectionChanged = oldCollection as INotifyCollectionChanged; if (notifyCollectionChanged != null) { notifyCollectionChanged.CollectionChanged -= ChildCollectionCollectionChanged; } foreach (T item in oldCollection) { this.Mutate(items => items.Remove(item)); } } } else if (e.Action == NotifyCollectionChangedAction.Replace) { foreach (IList oldCollection in e.OldItems) { INotifyCollectionChanged notifyCollectionChanged = oldCollection as INotifyCollectionChanged; if (notifyCollectionChanged != null) { notifyCollectionChanged.CollectionChanged -= ChildCollectionCollectionChanged; } } foreach (IList newCollection in e.NewItems) { INotifyCollectionChanged notifyCollectionChanged = newCollection as INotifyCollectionChanged; if (notifyCollectionChanged != null) { notifyCollectionChanged.CollectionChanged += ChildCollectionCollectionChanged; } } Rebuild(); } }
/// <summary> /// Unregisters a collection from the previous and next states of this operation. /// </summary> /// <param name="collection">The collection to unregister.</param> public void UnregisterCollection([NotNull] INotifyCollectionChanged collection) { previousState.UnregisterCollection(collection); nextState.UnregisterCollection(collection); }
public static CollectionProcessingVoid <TSourceItem> CollectionItemProcessing <TSourceItem>(this INotifyCollectionChanged source, Action <TSourceItem, CollectionProcessingVoid <TSourceItem> > newItemProcessor = null, Action <TSourceItem, CollectionProcessingVoid <TSourceItem> > oldItemProcessor = null, Action <TSourceItem, CollectionProcessingVoid <TSourceItem> > moveItemProcessor = null) { return(new CollectionProcessingVoid <TSourceItem>( source: source, newItemProcessor: newItemProcessor, oldItemProcessor: oldItemProcessor, moveItemProcessor: moveItemProcessor)); }
/// <summary> /// Invokes the specified handler for each change of the specified property on any of the elements in the specified collection. /// Note that the handler is not invoked for elements already present, nor if items are added where the property already has a value. /// </summary> /// <param name="collection"> The collection of elements on which to monitor property changes. </param> /// <param name="handler"> The action invoked when the property changes. </param> /// <param name="propertyName"> The name of the property to monitor. </param> /// <param name="handleNewItems"> Indicates whether the relevant property on an item added to the collection should be treated as though it changed. </param> public static void Bind(this INotifyCollectionChanged collection, string propertyName, PropertyChangedEventHandler handler, Type elementType = null, bool handleNewItems = false, PropertyChangedEventHandler onRemove = null) { Contract.Requires(collection != null); Contract.Requires(collection is System.Collections.IEnumerable); Contract.Requires(elementType == null || HasProperty(propertyName, elementType)); collection.CollectionChanged += collectionChanged; var collectionList = collection as System.Collections.IList ?? ((System.Collections.IEnumerable)collection).Cast <object>().ToList(); if (collectionList.Count != 0) { elementType = elementType ?? collectionList[0].GetType(); Contract.Requires(HasProperty(propertyName, elementType)); collectionChanged(collection, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collectionList)); } void collectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Move) { return; } if (e.NewItems != null) { foreach (var newItem in e.NewItems) { Contract.Requires(newItem is INotifyPropertyChanged, "This binding requires that all elements be non-null and implement INotifyPropertyChanged"); ((INotifyPropertyChanged)newItem).PropertyChanged += propertyChanged; if (handleNewItems) { handler(newItem, PropertyMutatedEventArgsExtensions.From(newItem, propertyName, elementType)); } } } if (e.OldItems != null) { foreach (var newItem in e.OldItems) { ((INotifyPropertyChanged)newItem).PropertyChanged -= propertyChanged; onRemove?.Invoke(newItem, PropertyMutatedEventArgsExtensions.From(newItem, propertyName, GetPropertyValue(newItem, propertyName, elementType), newValue: null, elementType)); } } } void propertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == propertyName) { handler(sender, e); } } }
private static INotifyCollectionChanged getSource(INotifyCollectionChanged source) { return(source.Casting <object>().Filtering(item => item is TResultItem)); }
public OfTypeComputing( INotifyCollectionChanged source) : base(getSource(source)) { _sourceOfTypeComputing = source; }
private static void AddAssociation(INotifyCollectionChanged collection, ChartSelectionBehavior behavior) { List <ChartSelectionBehavior> behaviors = GetOrCreateSelectionBehaviors(collection); behaviors.Add(behavior); }
public static void Raise <T>(this NotifyCollectionChangedEventHandler handler, INotifyCollectionChanged source, NotifyCollectionChangedAction action, T item, int index) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var args = new NotifyCollectionChangedEventArgs(action, item, index); handler?.Invoke(source, args); }