Click or drag to resize
CalculationWorks Logo
Custom Undo-Redo Repository
Custom Undo-Redo Repository

The example shows how to create a custom undo-redo repository.

With MyUndoRepository the undo depth is limited to 100.

To use MyUndoRepository add it as dataset behavior using BCF Editor.

public class MyUndoRepository : BcfDataSetBehaviorItemBase, IBcfDataSetUndoRepository {

    private readonly LinkedList<BcfTransaction> undoItemsField = new LinkedList<BcfTransaction>();
    private readonly LinkedList<BcfTransaction> redoItemsField = new LinkedList<BcfTransaction>();

    public int UndoItemCount { get { return UndoItems.Count; } }
    public int RedoItemCount { get { return RedoItems.Count; } }

    public LinkedList<BcfTransaction> UndoItems { get { return undoItemsField; } }

    public LinkedList<BcfTransaction> RedoItems { get { return redoItemsField; } }

    public void PushUndo(BcfTransaction transaction) {
        UndoItems.AddLast(transaction);
        if (UndoItems.Count > 100) UndoItems.RemoveFirst();
    }

    public void PushRedo(BcfTransaction transaction) { RedoItems.AddLast(transaction); }

    public BcfTransaction PopUndo() {
        var transaction = UndoItems.Last.Value;
        UndoItems.RemoveLast();
        return transaction;
    }

    public BcfTransaction PopRedo() {
        var transaction = RedoItems.Last.Value;
        RedoItems.RemoveLast();
        return transaction;
    }

    public void ClearUndo() { UndoItems.Clear(); }

    public void ClearRedo() { RedoItems.Clear(); }

    public bool CanUndo() { return UndoItemCount != 0; }

    public bool CanRedo() { return RedoItemCount != 0; }
}
See Also