0

I'm using a observable collection in my program to store "years". User can "Add" or "Remove" years from the collection. So "years" will be listed on ListBox. Window has "OK" and "Cancel". If user click "Cancel" button after entering years, I want to undo that changes and get back to original state, so user will see Original Observable Collection without any changes in next time he try to enter. Changes should only apply when click "OK" button. I implemented "OK" button correctly. What I want is to undo changes if click "cancel" button. Hope this is clear.

public RelayCommand Cancel
{
    get
    {
        return _cancel
            ?? (_cancel = new RelayCommand(
            () =>
            {
                foreach (var change in ChangesList)
                {
                    switch (change.Action)
                    {
                        case NotifyCollectionChangedAction.Add:
                            // What I have to do here
                            break;
                        case NotifyCollectionChangedAction.Remove:
                            // What I have to do here.
                            break;
                    }
                }
                Messenger.Default.Send(new NotificationMessage("Cancel"));
            }));
    }
}

Here is my cancel relay command. I have get changes that have made to the Original Observable collection into a "ChangeList". What I want is to undo those changes when click on "cancel" button.

CDspace
  • 2,639
  • 18
  • 30
  • 36
  • Question with code provided is not really that clear. But maybe store all changes user makes in a separate collection and if canceled, then publish it along with you `Cancel` message. Who-ever subscribes this message can then pop values out of the ObservableCollection. – Mikko Viitala Jun 30 '15 at 07:35

3 Answers3

1

make a copy of your origin observable collection:

var backup = collection.ToList();

when you need to cancel, just:

collection.Clear();
foreach (var i in backup)
    collection.Add(i);
Cologler
  • 724
  • 6
  • 18
  • Thank you for the reply. But I tried this. I use this collection to generate columns with each items. So, even though I clear and use backup list to fill original data, if I enter another item to the collection and click "OK" button, the whole list generate again, but not only newly added one. is that clear..? – Shashika Nanayakkara Jun 30 '15 at 11:51
  • @ShashikaNanayakkara did you mean edit property of year, when you click [OK] then change it back to the origin collection? then you should make a clone of year, enter year just edit the clone. after you click [OK], copy each member from clone to your origin year object. – Cologler Jul 01 '15 at 01:55
0

I never implemented this before, but I have seen it done. Cologer solution is one way to do it, but most of the solutions I looked at saved the changes to a temporary collection, and replaced the original collection when OK button is clicked. So when the cancel button is clicked you don't have to UNDO the changes made to the original collection as it remains unchanged through out the process.

tinku92
  • 109
  • 7
0

Thank you for all the help. Here is the solution to my answer. Its working the way I want now.

public RelayCommand Cancel
    {
        get
        {
            return _cancel
                ?? (_cancel = new RelayCommand(
                () =>
                {
                    foreach (var change in ChangesList)
                    {
                        switch (change.Action)
                        {
                            case NotifyCollectionChangedAction.Add:
                                Clone.Remove((T)change.NewItems[0]);
                                break;
                            case NotifyCollectionChangedAction.Remove:
                                Clone.Insert(change.OldStartingIndex, (T)change.OldItems[0]);
                                break;
                        }
                    }
                    Messenger.Default.Send(new NotificationMessage("Cancel"));
                }));
        }
    }
            }
        }

All you have to do is reverse the action you did in the "OK" button when you click "cancel" button