Question

I'm programming an application in WPF which uses the MVVM concept.

I'm trying to have no code-behind, and trying to keep my ViewModels and Views fully seperated. This means that I have to handle closing windows in xaml.

I want to execute a relaycommand, when I close the window, but only if I press "OK". If I press Cancel, the command must not be executed.

I have tried executing the relaycommand when the window on the trigger "IsClosing", but this doesn't work, because the command also is executed when "Cancel" is pressed. I know you can send the view as a parameter on relaycommand, but this means my ViewModel has knowledge of my View.

Does anyone know how to execute a relaycommand and close the window when I press "OK", without the ViewModel have any relation to the View?

Kind regards!

Was it helpful?

Solution

The best MVVM solution is to remove the window chrome (close button etc) and to provide explicit close and cancel buttons which you can bind to commands in the view-model.

To handle actually closing the window, use an attached behavior, as outlined in the accepted answer to this question - How should the ViewModel close the form?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top