/// <summary> /// Handles changes to the content of a ColumnDefinition in a ReportColumnCollection controls. /// </summary> /// <param name="sender">The object that originated the event.</param> /// <param name="collectionChangedEventArgs">The event arguments.</param> private void HandleColumnChanged(object sender, ColumnChangedEventArgs columnChangedEventArgs) { // The object that generated this event is a ReportColumnCollection. The main idea here is to store the action on the proper stack so that it // can be undone or redone if the need arises. Note that it is critical to tag the command with the state of Undoing or Redoing. ReportGrid reportGrid = sender as ReportGrid; // Two stacks maintain the state of undoing and redoing all actions within its scope. This will determine which commands will need to execute to // undo a given action. The actual act of undoing an operation will happen when the commands that are placed on the stacks here are executed. switch (columnChangedEventArgs.UndoAction) { case UndoAction.Create: // The act of creating an 'Undo' operation (as opposed to simply playing it back) will clear the Redo stack. That is, once a user udpates the // user interface, it is no longer possible to replay the stack forward. this.undoManager.RedoStack.Clear(); this.undoManager.UndoStack.Push(new CommandArgumentPair(UndoColumn, reportGrid, columnChangedEventArgs, UndoAction.Undo)); break; case UndoAction.Redo: // If the actions are being played forward, a command needs to be put back on the Undo stack in order to play it backwards. this.undoManager.UndoStack.Push(new CommandArgumentPair(UndoColumn, reportGrid, columnChangedEventArgs, UndoAction.Undo)); break; case UndoAction.Undo: // When playing a set of actions backwards, those actions are popped off the Undo stack. This, in turn, will push those same actions onto the // Redo stack so the operations can be played forward. this.undoManager.RedoStack.Push(new CommandArgumentPair(UndoColumn, reportGrid, columnChangedEventArgs, UndoAction.Redo)); break; } }
/// <summary> /// Undoes the most recent undo command on the stack. /// </summary> /// <param name="dependencyObject">The target of the undo operation.</param> private void UndoColumn(object sender, GenericEventArgs genericEventArgs) { // Extract the specific arguments from the generic arguments ReportGrid reportGrid = (ReportGrid)genericEventArgs.Arguments[0]; ColumnChangedEventArgs columnChangedEventArgs = (ColumnChangedEventArgs)genericEventArgs.Arguments[1]; UndoAction undoAction = (UndoAction)genericEventArgs.Arguments[2]; // This will undo a change to the width of a column. if (columnChangedEventArgs.DependencyProperty == ReportColumn.WidthProperty) { reportGrid.reportColumnCollection.SetColumnWidth(columnChangedEventArgs.ColumnDefinition, (Double)columnChangedEventArgs.OldValue, undoAction); } }