How to cancel multiple property changes

RogerSchlueter-7899 1,781 Reputation points
2026-07-19T00:41:38.44+00:00

I have a person class that is displayed in a wpf window so that a user can edit the various person properties. I want to allow a user to click on a button that cancels all changes up to that point. The following code is my implementation:

Private Sub SelectPerson(sender As Object, e As SelectionChangedEventArgs) Handles cbxPeople.SelectionChanged
	CurrentPerson = CType(cbxPeople.SelectedItem, Person)
	OriginalPerson = CurrentPerson.ShallowCopy
End Sub

Private Sub CancelChanges(sender As Object, e As RoutedEventArgs) Handles btnCancel.Click
	CurrentPerson = OriginalPerson
	OnPropertyChanged(NameOf(OriginalPerson))
End Sub

This works - the first time. But if the user makes additional edits then clicks on the cancel button again nothing happens. Any change are not reverted to their original values. What am I missing?

Developer technologies | VB

Answer accepted by question author
Taki Ly (WICLOUD CORPORATION) 4,440 Reputation points Microsoft External Staff Moderator
2026-07-20T03:46:59.9566667+00:00

Hello @RogerSchlueter-7899 ,

The issue you are experiencing is due to how reference types (like classes) work in .NET. When you click the Cancel button the first time, you are executing this line: CurrentPerson = OriginalPerson

Instead of copying the original values back, this line makes CurrentPerson point to the exact same object in memory as OriginalPerson. This means that when the user makes additional edits after the first cancel, they are now modifying the OriginalPerson object directly. The original state is permanently overwritten, so clicking Cancel a second time does nothing because the "backup" is already modified.

To fix this, you need to assign a new copy of OriginalPerson back to CurrentPerson when canceling, just like you did when initially selecting a person.

Additionally, your OnPropertyChanged event should notify the UI that CurrentPerson has changed, rather than OriginalPerson, assuming your UI is bound to CurrentPerson.

I suggest trying this implementation for your cancel button:

Private Sub CancelChanges(sender As Object, e As RoutedEventArgs) Handles btnCancel.Click
    ' 1. Revert by creating a fresh copy from the original object
    CurrentPerson = OriginalPerson.ShallowCopy()
    ' 2. Notify the UI that CurrentPerson has been updated
    OnPropertyChanged(NameOf(CurrentPerson))
End Sub

By using .ShallowCopy() in the cancel method, OriginalPerson will remain safely unmodified in memory regardless of how many times the user edits and cancels.

Hope this helps! Let me know if you have any further questions. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation.

Thank you.

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Newest
  1. Bruce (SqlWork.com) 85,281 Reputation points
    2026-07-19T16:22:17.5533333+00:00

    On the cancel changes you set current person to a reference instead of a copy, so changes to it are also changes to the original.

    Was this answer helpful?

    0 comments No comments

  2. AI answer

    2026-07-19T00:41:54.0166667+00:00

    OnPropertyChanged(NameOf(OriginalPerson)) only notifies that the OriginalPerson property changed. It does not restore the edited properties on the existing Person object that the UI is bound to.

    In WPF binding, each property that should update the UI must raise property change notifications, and canceling an edit requires restoring the original values of the edited item, not just replacing a backup reference.

    Two supported approaches are shown by the context:

    1. Restore each property value on the current object
      • Copy the saved values from OriginalPerson back into CurrentPerson.
      • Ensure the Person class implements INotifyPropertyChanged and raises PropertyChanged for each property when updated.
    2. Use edit-transaction support
      • If editing is done through a collection view that supports IEditableCollectionView, call CancelEdit().
      • CancelEdit() ends the edit transaction and restores the original values when CanCancelEdit is True.

    For the pattern shown, the first approach matches the current code structure better. The key issue is that CurrentPerson = OriginalPerson replaces the reference, but later edits are happening on the bound object, and the backup is not being reapplied property-by-property with notifications.

    A typical fix is:

    Private Sub SelectPerson(sender As Object, e As SelectionChangedEventArgs) Handles cbxPeople.SelectionChanged
        CurrentPerson = CType(cbxPeople.SelectedItem, Person)
        OriginalPerson = CurrentPerson.ShallowCopy()
    End Sub
    
    Private Sub CancelChanges(sender As Object, e As RoutedEventArgs) Handles btnCancel.Click
        CurrentPerson.FirstName = OriginalPerson.FirstName
        CurrentPerson.LastName = OriginalPerson.LastName
        CurrentPerson.Age = OriginalPerson.Age
        ' Repeat for each editable property
    End Sub
    

    Also make sure the Person properties raise PropertyChanged when set. In WPF, that is required for OneWay or TwoWay binding to automatically reflect dynamic changes.

    If the goal is repeated cancel support during an edit session, keep the original snapshot unchanged until the edit is committed, and refresh OriginalPerson = CurrentPerson.ShallowCopy() only when a new person is selected or after saving.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.