0

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!

Jonas Jensen
  • 77
  • 2
  • 9

1 Answers1

0

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?

Community
  • 1
  • 1
Peregrine
  • 4,287
  • 3
  • 17
  • 34