Handle Window Close

Feb 7, 2012 at 1:08 AM

Hi,

I have a data control (UserControl) that I use with my 'data' windows (allows add, delete, save, refresh etc actions to the viewmodel behind the window).

The control handles 'dirty object' issues. EG user edits viewmodel data then attempts to refresh view without first saving the object.

Before moving to MVVM, I had the control intercept the window close event to handle cases where the user closed a window with 'dirty' data. This was all done with code behind in the data control.

What do you recommend in MVVM & Catel.?

Feb 7, 2012 at 1:13 AM

Sorry,  I forgot to mention:

  • in most cases the 'data window' is non modal.
  • Also,the 'data window' is derived from the Catel:DataWindow.
Coordinator
Feb 7, 2012 at 7:44 AM

The save button of the data window calls ViewModelBase.SaveViewModel(). Then you can return false, which means that the save action has failed and the window will not be closed.

I hope this is what you need.

Feb 7, 2012 at 7:21 PM

My 'data' windows do not display/use the catel:DataWindow buttons. They use my data control. Handling user actions on the control is all ok.

My issue is how to best handle the case where the user closes the window directly. In this case my control doesn't see the user action. As I said, I was subscribing to the window closing event. My question is whether this is the best way to go in catel.

 

 

 

Coordinator
Feb 7, 2012 at 8:19 PM

Does the DataWindow host a view model directly, or does it only host the control and has the control a view model?

In the first case, I recommend to add a check in the Closing event to make sure that the ViewModel.HasErrors is false. If not, do not allow the closing of the window. 

In the second case, in the closing event ask the user control whether it allows you to close. This all has to be done in the code-behind, not in the view model (because this is view-specific code).

Best thing to do is to create your own window, derive that from the DataWindow and then derive all your windows from that one. Then you can easily insert new functionality into all your windows.

Feb 7, 2012 at 9:45 PM

Thanks for the quick response. You gave me the prompt I needed.

I have now derived a new window from DataWindow which overrides OnClosing(). The window has direct access to the viewmodel. OnClosing() calls the view model HasDirtyModel property.

I have also derived a new class from ViewModelBase which overrides HasDirtyModel & provided the reference to my 'std data object ctlr'. This ctlr is the view model 'back end' to my data control which sits on the window or a control within the window.

Now adding new view models & windows with 'my std behaviour' is a breeze.

 

Dec 12, 2013 at 10:28 AM
GeertvanHorrik, when I call CloseViewModel from my ViewModel, my Command members (e.g. for controlling buttons) are nulled and then OnClosing() is called. When I set e.Cancel = true in OnClosing method, and DataWindow stays alive, user can't control DataWindow via buttons, because command members are null. What am I doing wrong? Thank you very much for the help. B.
Coordinator
Dec 12, 2013 at 11:45 AM
Good question. Please create a ticket in our issue tracker and we will fix this in the next release.
Dec 12, 2013 at 12:24 PM
I can't create new ticket because of "Your account is not yet available in this OnDemand instance." error durning login into the issue tracker.

Anyway, is there any way how can I solve this problem? It does not help to set commands with new ones...
Coordinator
Dec 12, 2013 at 12:27 PM
Did you try this one:

https://catelproject.atlassian.net/secure/Signup!default.jspa

Anyway, probably the VM is killed anyway and set to null.
Dec 12, 2013 at 12:58 PM
Thank you, I created new ticked.
VM is not killed, I can still access it.