I have created a calculator like function to allow the user to update all cells in a given column. I loop through each cell in the column and change the value based on user input. What I would like is to have the ability for the user to undo what was done to each cell in one undo click. Is this possible with the built in undo button, or would a custom undo be required? If the latter, what properties should I be looking at to make it work?
I was looking at using the DataValueChanged Event with its history capabilities to possibly put something together. I will submit the request and maybe there will be a more elegant solution in the future. Thanks for the help.
Setting the Value does not automatically add it to the grid's undo stack. Instead there are overloads of the DataRecord's SetCellValue that take a boolean indicating whether to add the change operation to the undo stack. You probably don't want to go this route because if the user presses ctrl-z an undo will occur but for only the last item in the undo stack (i.e. the last cell edited). Currently there is no support for creating custom undo operations or aggregating cell changes into a single operation so I would recommend submitting a suggestion for this.
One possible way to acheive this in the interim would be to have an unbound field in the fieldlayout and then set the value of 1 cell using this overload. You would have to watch the grid's events (e.g. CellUpdated) for when the value of the cell reverts but then use that as your trigger for undoing the changes to the cells.
It seems that the code:
xdg.ExecuteCommand(DataPresenterCommands.Undo);
will not undo a cell change if I change the cell in code with something like:
Cell targetCell = GetCellToChange();
targetCell.Value = newValue;
but it will undo a cell change if I manually type in the cell.
Hello,
What you can do is call the DataPresenterCommands.Undo as many times as the number of changes you have made. However, currently there is no property exposed on the XamDataGrid to know how many actions you should undo (please note that actions like resizing, etc can also be undo-ed).
private void button1_Click(object sender, RoutedEventArgs e)
{
}
You could create a custom code that will keep track of this.
You may also want to submit a feature request for adding a property or a collection, showing how many actions can be undo-ed here.