tags:

views:

433

answers:

2

I like the MVVM idea of a RelayCommand that's exposed by the ViewModel. That's nice and elegant, for operations that can be done with no further user input. Simple. Testable.

However, not all operations are UI-less. Some require confirmation ("Are you sure you want to delete?"). Others require more information yet. Opening a file might entail anything from a File Open dialog up through a full-blown Import wizard.

What's the best way, within an MVVM app, to write commands that require user input? Is there an established pattern for solving this with dependency injection somehow? Should I write a KeyDown handler in the code-behind, and have it explicitly execute the event? Should I fall back on RoutedUICommand, and put all the "display the next GUI" code in my View? Or is there something obvious that I'm totally missing?

+3  A: 

I usually use Dependency Injection to inject some kind of abstract IShowTheInterface thing, and then invoke methods on the abstraction from within the Command. These methods should then give you the answers you need to determine whether to continue with the action at all, and what input the user gave.

I recently used this as an example in a blog post with a bit of a different topic.

Mark Seemann
A: 

This kind of stuff (confirmation dialogs, file open dialogs etc.) are usually shared between applications. So my preference is not put them in the ViewModel at all.

ViewModel is app-specific, and it's not a good idea to expand ViewModelBase indefinitely. Instead, create re-usable behaviours to extend the View. There are quete a few behaviour samples at the Expression Blend Gallery

EDIT:

Behaviors can have properties and you can use these properties not only to specify behavior characteristics, but also to get some feedback:

<Button Content="Open Document">
    <i:Interaction.Behaviors>
        <local:FileOpenBehavior 
            FileNameTarget="{Binding ElementName=tbDocName}"/>
    </i:Interaction.Behaviors>
</Button>

In the example above, tbDocName can be hidden - or you can bind to a property of your ModelView.

Sergey Aldoukhov
The "show an Open dialog" part may be reusable; but then you need to do something with that file, and that's going to be application-specific, so I'm not sure I see how this works. Can you give an example of how you would do this with behaviors?
Joe White