Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 doing:

new WhateverWindow( ).Show( ) 

in the ViewModel seems wrong.

Cheers,

Steve

like image 671
Steve Dunn Avatar asked Mar 17 '09 16:03

Steve Dunn


1 Answers

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.

like image 105
Nikhil Kothari Avatar answered Sep 28 '22 16:09

Nikhil Kothari