Esempio n. 1
0
        /// <summary>
        /// Called when a property has changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// This method must be public because the <see cref="IWeakEventListener"/> is used.
        /// </remarks>
        public void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var modelBase = sender as ModelBase;

            if (modelBase != null)
            {
                if ((string.CompareOrdinal(e.PropertyName, "INotifyDataErrorInfo.HasErrors") == 0) ||
                    (string.CompareOrdinal(e.PropertyName, "INotifyDataWarningInfo.HasWarnings") == 0) ||
                    (string.CompareOrdinal(e.PropertyName, "IsDirty") == 0))
                {
                    return;
                }
            }

            if (ShouldPropertyBeIgnored(sender, e.PropertyName))
            {
                return;
            }

            var oldValue = _previousPropertyValues[e.PropertyName];
            var newValue = PropertyHelper.GetPropertyValue(sender, e.PropertyName, false);

            // CTL-719: ignore duplicate properties
            if (ObjectHelper.AreEqual(oldValue, newValue))
            {
                return;
            }

            _previousPropertyValues[e.PropertyName] = newValue;

            MementoService.Add(new PropertyChangeUndo(sender, e.PropertyName, oldValue, newValue, Tag));
        }
        /// <summary>
        /// This is invoked when the collection changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
        /// <remarks>
        /// This method must be public because the <see cref="IWeakEventListener"/> is used.
        /// </remarks>
        public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            Log.Debug("Automatically tracking change '{0}' of collection", e.Action);

            var undoList   = new List <CollectionChangeUndo>();
            var collection = (IList)sender;

            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                undoList.AddRange(e.NewItems.Cast <object>().Select((item, i) => new CollectionChangeUndo(collection, CollectionChangeType.Add, -1, e.NewStartingIndex + i, null, item, Tag)));
                break;

            case NotifyCollectionChangedAction.Remove:
                undoList.AddRange(e.OldItems.Cast <object>().Select((item, i) => new CollectionChangeUndo(collection, CollectionChangeType.Remove, e.OldStartingIndex + i, -1, item, null, Tag)));
                break;

            case NotifyCollectionChangedAction.Replace:
                undoList.Add(new CollectionChangeUndo(collection, CollectionChangeType.Replace, e.OldStartingIndex, e.NewStartingIndex, e.OldItems[0], e.NewItems[0], Tag));
                break;

#if NET
            case NotifyCollectionChangedAction.Move:
                undoList.Add(new CollectionChangeUndo(collection, CollectionChangeType.Move, e.OldStartingIndex, e.NewStartingIndex, e.NewItems[0], e.NewItems[0], Tag));
                break;
#endif
            }

            foreach (var operation in undoList)
            {
                MementoService.Add(operation);
            }

            Log.Debug("Automatically tracked change '{0}' of collection", e.Action);
        }