3

I've a WPF application which allows me to edit some data.

I would like to make that if we try to close the application, the user must acknowledge that he will lost its modifications.

But here we are, I got several problem:

  1. There is no "Closing" commands on the windows object(I can execute a command when I have an event from the code behind I guess)
  2. I don't know how it's the recommended way to cancel something with the MVVM pattern? Normally I would have put the e.Cancel = true;, but we can't because it's a command

So how would you ask the user if he is sure to close the windows, with the MVVM pattern?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • One thing to note here is that just because you're using the MVVM pattern, it doesn't mean you _can't_ use events - just that you should generally try to avoid it where it makes more sense to use commands. As a rule, I create my own classes to extend basically every component (window, textbox, etc) in a utility DLL, and use those. That way, if I need an event to fire for something like this, I don't have to re-invent the wheel each time. – sybkar Nov 29 '12 at 13:19

2 Answers2

0

The concept is to add a behavior to your window that "hooks" into the window closing event. Once the behavior is hooked in, you can perform just about any action you need without violating the principles of MVVM. Check this link for information on how to create a window closing behavior:

http://gallery.expression.microsoft.com/WindowCloseBehavior/

Hope that helps.

Backlash
  • 541
  • 3
  • 8
  • Thank you for this post, it has been very instructive to me. I've a question, in my case, I've to ask the user if he really wants to close the windows. If I follow your example, I should but the MessageBox query in the CanExecute. The problem is that the execution of this CanExecute will start some MessageBox, and the framework may try execute the "CanExecute" frequently, displaying a lot of popup to my users. I've download the example of your link, I modified it a little bit, to ask the user if he wants to close, and add a button with this close commands http://u.j4n.ch/TrXFbs – J4N Nov 29 '12 at 13:55
  • Following the code sample literally will ask you to put the message box query in the CanExecute method, but as you can see, that is not optimal. I would deviate from the example in that case and process your cancel variable to be a result based on your message box. For example, in the Window_Closing event, display your message box, if the user accepts the close, then execute your close command. If the user elects to cancel the close, then set the e.Cancel event property to true and let the code exit out. – Backlash Nov 29 '12 at 14:28
  • Yes I know that I can do that, but making presentation choice is for something to do on the ViewModel, and not in the code behind, and the primary goal of this project is to fully apply the MVVM concept – J4N Nov 29 '12 at 15:06
  • Behaviors are not code behind. Behaviors are a way to encapsulate functionality into a reusable component. I fail to see how the implementation of a behavior breaks the MVVM concept? Perhaps I'm not following what you mean by "making a presentation choice is for something to do on the ViewModel"? What presentation choice are you referring to? – Backlash Nov 29 '12 at 15:35
  • You say that I should do stuff in the Window_Closing(so in the code behind). I'm talking about everything that is in this method. – J4N Nov 30 '12 at 06:48
  • Ahh now I understand the confusion. I meant the window_closing handler you create in the behavior, not in the code-behind. – Backlash Nov 30 '12 at 13:48
0

Since you tagged this question as "mvvm-light", you can check the EventToCommand in MVVMLight. It may meet your needs.

This question is similar:

Handling the window closing event with WPF / MVVM Light Toolkit

Community
  • 1
  • 1
Johnsonlu
  • 112
  • 1
  • 10
  • Yes, I'm using MVVM LIght, but the problem is that once the event is transformed into a command, I dont have any parameter left on which I can do a e.Cancel=true; – J4N Nov 29 '12 at 15:04
  • 1
    @J4N: Did you set PassEventArgsToCommand="True" in EventToCommand? – Johnsonlu Nov 30 '12 at 00:56