Exemple #1
0
        /// <summary>
        ///     Get (or create) an UndoRoot for the specified object or document instance.
        /// </summary>
        /// <param name="root">The object that represents the root of the document or object hierarchy.</param>
        /// <returns>An UndoRoot instance for this object.</returns>
        public UndoRoot this[object?root]
        {
            get
            {
                if (null == root)
                {
                    return(DummyUndoRoot);
                }

                UndoRoot?ret = null;

                if (_roots.ContainsKey(root))
                {
                    ret = _roots[root];
                }

                if (null == ret)
                {
                    ret = new UndoRoot(root);
                    _roots.Add(root, ret);
                }

                return(ret);
            }
        }
        /// <summary>
        ///     Construct a Change instance with actions for undo / redo.
        /// </summary>
        /// <param name="instance">The instance that changed.</param>
        /// <param name="propertyName">The property name that changed. (Case sensitive, used by reflection.)</param>
        /// <param name="oldValue">The old value of the property.</param>
        /// <param name="newValue">The new value of the property.</param>
        /// <param name="descriptionOfChange">A description of this change.</param>
        public virtual void OnChanging(object instance, string propertyName, object?oldValue, object?newValue,
                                       string descriptionOfChange)
        {
            var supportsUndo = instance as ISupportsUndo;

            if (null == supportsUndo)
            {
                return;
            }

            object?root = supportsUndo.GetUndoRoot();

            if (null == root)
            {
                return;
            }

            // Add the changes to the UndoRoot
            UndoRoot undoRoot = UndoService.Current[root];

            if (undoRoot.IsUndoingOrRedoing)
            {
                return;
            }

            Change?change = GetChange(instance, propertyName, oldValue, newValue);

            if (change is null)
            {
                return;
            }
            undoRoot.AddChange(change, descriptionOfChange);
        }
Exemple #3
0
        /// <summary>
        ///     Starts an undo batch, which is ended when this instance is disposed. Designed for use in a using statement.
        /// </summary>
        /// <param name="root">The UndoRoot related to this instance.</param>
        /// <param name="description">The description of this batch of changes.</param>
        /// <param name="consolidateChangesForSameInstance">Should the batch consolidate changes.</param>
        public UndoBatch(UndoRoot root, string description, bool consolidateChangesForSameInstance)
        {
            if (null == root)
            {
                return;
            }

            _undoRoot = root;
            root.BeginChangeSetBatch(description, consolidateChangesForSameInstance);
        }
Exemple #4
0
        /// <summary>
        ///     Create a ChangeSet for the specified UndoRoot.
        /// </summary>
        /// <param name="undoRoot">The UndoRoot that this ChangeSet belongs to.</param>
        /// <param name="description">A description of the change.</param>
        /// <param name="change">The Change instance that can perform the undo / redo as needed.</param>
        public ChangeSet(UndoRoot undoRoot, string description, Change change)
        {
            _undoRoot    = undoRoot;
            _changes     = new List <Change>();
            _description = description;

            if (null != change)
            {
                AddChange(change);
            }
        }
        /// <summary>
        ///     Construct a Change instance with actions for undo / redo.
        ///     Returns True, if is Undoing or Redoing, otherwise False.
        /// </summary>
        /// <param name="instance">The instance that changed.</param>
        /// <param name="propertyName">
        ///     The property name that exposes the collection that changed. (Case sensitive, used by
        ///     reflection.)
        /// </param>
        /// <param name="collection">The collection that had an item added / removed.</param>
        /// <param name="e">The NotifyCollectionChangedEventArgs event args parameter, with info about the collection change.</param>
        /// <param name="descriptionOfChange">A description of the change.</param>
        public virtual bool OnCollectionChanged(object instance, string propertyName, object collection,
                                                NotifyCollectionChangedEventArgs e, string descriptionOfChange)
        {
            ISupportsUndo?supportsUndo = instance as ISupportsUndo;

            if (null == supportsUndo)
            {
                return(false);
            }

            object?root = supportsUndo.GetUndoRoot();

            if (null == root)
            {
                return(false);
            }

            // Add the changes to the UndoRoot
            UndoRoot undoRoot = UndoService.Current[root];

            if (undoRoot.IsUndoingOrRedoing)
            {
                return(true);
            }

            // Create the Change instances.
            IList <Change>?changes = GetCollectionChange(instance, propertyName, collection, e);

            if (null == changes)
            {
                return(false);
            }

            foreach (Change change in changes)
            {
                undoRoot.AddChange(change, descriptionOfChange);
            }

            return(false);
        }