R – Best place to bring up new window in Model View ViewModel


I have an MVVM application. In one of the ViewModels is the 'FindFilesCommand' which populates an ObservableCollection. I then implement a 'RemoveFilesCommand' in the same ViewModel. This command then brings up a window to get some more user input.

Where/what is the best way to do this whilst keeping with the MVVM paradigm? Somehow

new WhateverWindow( ).Show( ) 

in the ViewModel seems wrong.



Best Solution

I personally look at this scenario as one where the main window view model wants to surface a task for the end user to complete.

It should be responsible for creating the task, and initializing it. The view should be responsible for creating and showing the child window, and using the task as the newly instantiated window's view model.

The task can be canceled or committed. It raises a notification when it is completed.

The window uses the notification to close itself. The parent view model uses the notification to do additional work once the task has committed if there is followup work.

I believe this is as close to the natural/intuitive thing people do with their code-behind approach, but refactored to split the UI-independent concerns into a view model, without introducing additional conceptual overhead such as services etc.

I have an implementation of this for Silverlight. See http://www.nikhilk.net/ViewModel-Dialogs-Task-Pattern.aspx for more details... I'd love to hear comments/further suggestions on this.