Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to display pop-ups using WPF M-V-VM pattern

I had a question about the VM responsibilities when it comes to pop-ups. When an app is popping a message box or some kind of dialog (with MVVM), the two options that we have are:

  1. putting UI (ShowDialog()) code in VM which seems bad
  2. have VM send some kind of event that UI can subscribe to and display a dialog in the code behind (but we are striving for zero code behind :) )

How do you guys tackle this case?

like image 476
Lenik Avatar asked Apr 02 '09 00:04

Lenik


2 Answers

Don't put the UI code in the VM, that just causes a lot of headaches down the road.

You usually have two cases when you want to pop a window or dialogue. Either you are doing it because of a business case, e.g. a detail view on double clicking a list, or it is completely UI based, e.g. popping an options window. In the first case it's best to use an event in the VM, in the later case I just use an event handler. A good rule of thumb is, if you don't need any (significant) VM variables to accomplish the action then you should just use an event handler.

Above all, use your head and trust your judgment, you'll learn which to use soon enough.

like image 84
Bryan Anderson Avatar answered Nov 11 '22 14:11

Bryan Anderson


Check out Onyx. This is an M-V-VM library (full disclosure: I'm the author) based on using services and either the Service Locator or Dependency Injection patterns. There's services for MessageBox and common dialogs, and it's pretty easy to add your own services as well.

like image 35
wekempf Avatar answered Nov 11 '22 14:11

wekempf