0

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)?

TheBlindSpring
  • 631
  • 5
  • 22

2 Answers2

0

Since you do not know where or how then you must use a debugger or a a thread which has access to the memory space of the variable. In the case of the debugger Add a watch and break on access / read write in the latter use a thread and sleep until something != somethingElse == true; then perform logic after to debug. Depending on the level of access you have in the system you may be able to also ascertain the function call and the caller using some constants but lesser knowns.. How to get the name of the current method from code

or

[CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)

You can also use a the Watch window

Community
  • 1
  • 1
Jay
  • 3,276
  • 1
  • 28
  • 38
0

The problem is that while 'saving' the old value, it simple saves itself as a reference inside your variable 'undoTool'. Because its a reference it doesn't do anything else then referencing to its base object which is the value you are editing. This means whenever you change the base object all references to it will return the same value as the one you've changed it into.

You should tell specifically tell .NET to create a new object, thus using the new keyword, and then store your variable inside it.

Jevgeni Geurtsen
  • 3,133
  • 4
  • 17
  • 35
  • I am sorry, I don't know where the new keyword goes. Do I put "undoTool = new ToolClass(); undoTool = toolsList[selectedToolNdx];" in the cell edit ending? – TheBlindSpring May 21 '14 at 16:49
  • 1
    I believe so. Another way would be implementing `ICloneable` to your ToolClass and just use `toolsList[selectedToolNdx].Clone()`. Do you want to save the whole object or just its text? Because if you want to save the text you'd be better of using a string. – Jevgeni Geurtsen May 21 '14 at 16:58