tags:

views:

394

answers:

2

With MVVM and WPF what would be a good/straightforward way to handle opening and closing new windows and dialogs? Opening and closing should be driven by the ViewModel right? But the ViewModel should not know about the view ...

+3  A: 

I usually use interfaces for this. For example, if i want to edit a record in a separate window, i have an interface IEditingProvider<TViewModel>, which i can implement somewhere else and then pass an interface reference to the constructor of my ViewModel. The EditingProvider might just do something like this:

class MyRecordEditingProvider: IEditingProvider<MyRecordViewModel>
{
    // Implementation of generic interface method
    public void Edit(MyRecordViewModel model) {
        EditWindow edit = new EditWindow(); 
        edit.DataContext = model;
        edit.ShowDialog();
    }
}
Botz3000
How an where do you maintain the EditingProviders that are available to (a certain part of) your application and how do you retrieve the correct instance that you eventually will pass to the ViewModel's constructor? I suppose it is not all hardwired but decoupled?
bitbonk
Yes, it is decoupled. Actually i'm using a Dependency Injection Framework (Composite Application Block by Microsoft) for mapping the generic interfaces to implementations. I am currently doing that in code, but the Unity Container can also be configured using a configuration file.
Botz3000
A: 

I use Controllers for this. They are responsible for the application workflow and so they open and close Windows/Dialogs.

The ViewModel sample application of the WPF Application Framework (WAF) shows how to solve this scenario.

jbe