How to cancel the cancellation in the view model when using a datawindow?

Topics: Questions
Jul 23, 2012 at 8:08 PM

When using a default Ok/Cancel Datawindow, the Save method of the ViewModel can be overridden to cancel the save when the user pressed OK (when there are errors or some other stuff).

This doesn't seem possible with the Cancel method? The overridable Save method returns a boolean to prevent closure, but the overridable Cancel method doesn't have this boolean.

So how can I prevent the window from being cancelled? For example when you enter some data in a detail window and the user presses cancel, one could issue a message to the user that his modifications will be lost. He should have the possibility to 'cancel the cancel' so his edits are not lost.

How to do this?

Regards,
Peter

Jul 25, 2012 at 9:06 AM

It seems I have no choice but to create a DataWindow with custom button and not use the default OK/Cancel buttons. And then implement custom commands in the viewmodel.

Coordinator
Jul 25, 2012 at 5:50 PM

There is no option in Catel to cancel the cancel. If you want this, please create an issue in the issue tracker.

Jul 25, 2012 at 6:37 PM

Issue has been created :)

Jul 30, 2012 at 12:47 PM

I now have the Cancel method of the ViewModel overridden in which I use the IMessageService to display an 'are you sure message'. I return false when I want the cancel to be cancelled but the Cancel method itself always seems to be called twice (so two 'are you sure' messages always appear)?

Am I doing something wrong here?

Coordinator
Jul 30, 2012 at 2:17 PM

Shouldn't be happening. Can you post a repro?

Jul 30, 2012 at 3:01 PM

Found the little bugger. It's not a bug. I had to override DiscardChanges in my custom DataWindow also, because it is always called in the OnDataWindowClosing event. I have overridden it and left it empty so all is working well now. It's the risc of creating a datawindow subclass, but I see no other alternative.

Coordinator
Jul 30, 2012 at 3:08 PM

Subclassing the datawindow shouldn't be hard. The default behavior of DiscardChanges does not work for you?

Jul 30, 2012 at 4:21 PM
Edited Jul 30, 2012 at 4:26 PM

The default behaviour of DiscardChanges itself is just fine. The problem is that it is also called in OnDataWindowClosing.

if (!ClosedByButton)
{
   DiscardChanges();
}

Because I have my own custom buttons, 'ClosedByButton' is false (can't set the 'ClosedByButton' property in my subclass because it's private instead of protected) and therefore DiscardChanges (which calls CancelViewModel) is called again e.g. twice in a row.

Edit: just read your reaction in http://catel.codeplex.com/discussions/389456. This will solve the issue because I can reuse OnOkExecute and then ClosedByButton will be set accordingly :)