1

I have a window, and its datacontext is a viewmodel. now, outside the scope of the vm and window(view), i have a method that will be called. the method needs to show the window and let the user change some things, then when the user is done, the window needs to close and the method continue. tricky thing is, i have buttons on the window bound to commands in the viewmodel. these commands fire events like 'userCancelled' and 'userOked'. so i want to catch these events to know when to close the window and continue with the method.

on the window if i use ShowDialog, the events aren't caught (i'm assuming because the thread is tied up waiting on the dialog to close). But if i use Show, the method rolls on. i need it to wait. i've tried Thread.Sleep until some boolean value is switched on, but that ties up the thread too and makes the window unusable (using .Show()).

I've tried making a backgroundWorker, but i still run into the same problem.

i understand i could use codebehind on the buttons, but for the OK button, the viewModel has to verify that its state is valid. having the view consult with the viewmodel defeats the point of MVVM's loose coupling style, right?

I'm learning WPF and MVVM all on my own, and i want to do it right and stick to good design principals where practical. how should i do this? what am i doing wrong? thanks.

prologue_1
  • 27
  • 6
  • FYI right now i've just added a Window property to the ViewModel and i'm calling window.close() in the viewmodel when needed. I'd love to know the "right" way to do this in MVVM. – prologue_1 Apr 06 '12 at 14:49
  • update: created a CloseWindow Action in the view model and upon tying the window and viewmodel together, setting that action equals to the window's Close() method via anonymous delegate (i think? :) will study up on prism as time permits. – prologue_1 Apr 09 '12 at 14:15

1 Answers1

1

You can correctly do this using Prism and a WindowRegionAdapter.

  1. Register the view(user control) that you want to show/hide with the WindowRegionAdapter so that when the view is requested it opens up in a new window.
  2. Create CompositePresentationEvents for Show/Hide/Close of the view and put them in some common infrastructure assembly.
  3. In the Initialize method of the the module containing the view, subscribe to the events created in 2 and and link them to OnShow/OnHide/OnClose event handlers.
  4. In the event handlers you can use the Prism region manager to add/remove the view from the region which will in turn show/hide/close the window. You can modify the WindowRegionAdapter to do precisely what you want it to do.
  5. Then whenever you want to show/hide the view you can simply publish the appropriate show/hide events wherever (in whichever module/viewmodel) you want and the appropriate event handler will be called in the module subscribing to the event and the window will be shown hidden.

Yes its slightly complicated but absolutely worth the investment especially if you need open other windows as well. Keeps things very clean.

NVM
  • 5,442
  • 4
  • 41
  • 61
  • studying up on prism now. thanks for the tip. any recommended tutorials to get me boned up on it? – prologue_1 Apr 09 '12 at 12:17
  • Have a look at the links in [this question](http://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish). – NVM Apr 09 '12 at 16:42