Esempio n. 1
0
 /// <summary>
 /// Invokes an action for each item in a collection and subsequently each item added or
 /// removed from the collection.
 /// </summary>
 /// <typeparam name="T">The type of the collection items.</typeparam>
 /// <param name="collection">The collection.</param>
 /// <param name="added">
 /// An action called initially for each item in the collection and subsequently for each
 /// item added to the collection. The parameters passed are the index in the collection and
 /// the item.
 /// </param>
 /// <param name="removed">
 /// An action called for each item removed from the collection. The parameters passed are
 /// the index in the collection and the item.
 /// </param>
 /// <param name="reset">
 /// An action called when the collection is reset.
 /// </param>
 /// <returns>A disposable used to terminate the subscription.</returns>
 public static IDisposable ForEachItem <T>(
     this IPerspexReadOnlyList <T> collection,
     Action <T> added,
     Action <T> removed,
     Action reset)
 {
     return(collection.ForEachItem((_, i) => added(i), (_, i) => removed(i), reset));
 }
Esempio n. 2
0
        /// <summary>
        /// Listens for property changed events from all items in a collection.
        /// </summary>
        /// <typeparam name="T">The type of the collection items.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="callback">A callback to call for each property changed event.</param>
        /// <returns>A disposable used to terminate the subscription.</returns>
        public static IDisposable TrackItemPropertyChanged <T>(
            this IPerspexReadOnlyList <T> collection,
            Action <Tuple <object, PropertyChangedEventArgs> > callback)
        {
            List <INotifyPropertyChanged> tracked = new List <INotifyPropertyChanged>();

            PropertyChangedEventHandler handler = (s, e) =>
            {
                callback(Tuple.Create(s, e));
            };

            collection.ForEachItem(
                x =>
            {
                var inpc = x as INotifyPropertyChanged;

                if (inpc != null)
                {
                    inpc.PropertyChanged += handler;
                    tracked.Add(inpc);
                }
            },
                x =>
            {
                var inpc = x as INotifyPropertyChanged;

                if (inpc != null)
                {
                    inpc.PropertyChanged -= handler;
                    tracked.Remove(inpc);
                }
            },
                null);

            return(Disposable.Create(() =>
            {
                foreach (var i in tracked)
                {
                    i.PropertyChanged -= handler;
                }
            }));
        }