I am working on implementing a undo function for my WPF datagrid, however I a running into problems. I am sure it is just a misunderstanding of the C# language and its syntax, but I don't know what the problem is. When the cell edit is ending, I obtain the previous item and display an item value (Z) (for debugging purposes). It contains the item held BEFORE the edit (which is what I want). But when I try to 'undo' it, I display the SAME value (Z) again but it has changed to the current value of that item in the datagrid.
CellEditEnding Handler
private void toolGrid_CellEditEnding(object sender, DataGridVellEditEndingEventArgs e)
{
undoTool = toolsList[selectedToolNdx];
MessageBox.Show(undoTool.Z.ToString());
}
KeyDown Handler
private void toolGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (e.Key == Key.Z)
{
MessageBox.Show(undoTool.Z.ToString());
e.Handled = true;
}
}
}
So for instance, I can change the z-value from 3 to 5, and the celleditending event fires and a messagebox shows the value '3' (so far so good). However, I then undo (CTRL-Z) and a messagebox now shows the value '5' even though it should still be 3. Why is the undoTool changing (and why does it seem to be changing by itself)?