File Open Dialog from ViewModel?

Topics: Prism v2 - WPF 3.5
Oct 20, 2009 at 4:24 PM

If the ViewModel isn't supposed to know anything about the view (and vice-versa), then if I need to show a file open dialog in response to a button click (and subsequent associated command), where and how should this occur?

Oct 21, 2009 at 7:25 PM

I put it in the View using a click event because i felt that since the dialog just populates a textbox, and that another button will fire a command that will actually do something with that data, that the call to the dialog belonged in the view.

Oct 23, 2009 at 9:15 PM
Edited Oct 23, 2009 at 9:16 PM

I suggest placing it in a class that can be used in a command that can be executed from the ViewModel. As an example, you would have the following delegate in the ViewModel:

public DelegateCommand<object> OpenFileCommand { get; private set; }

Which is bound in the ViewModel constructor with an Action and a Predicate.


            AddFileCommand = new DelegateCommand<object>(AddFile, _importDataSvc.CanAddFile);

OpenFileCommand = new DelegateCommand<object>(OpenFile, CanOpenFile);

The Action would be the method that calls the sub-classed open dialog...




Oct 26, 2009 at 3:10 PM

I don't agree.  The action is not opening the file per se.  All it's doing is filling a textbox with the file path.  There is another button on the form that will actually perform the intended command.  That command will take all the data from the form, and pass it along.  The open file action is not germane to the ViewModel.

Oct 27, 2009 at 7:57 AM

You can check this


Oct 27, 2009 at 1:55 PM

OK however the VM in the MVVM triad should be agnostic to the V.  Therefore there shouldn't any knowledge of how to get the data into it's respective properties.  I should, theoretically, be able to wire-up my VM to a WinForms V or an ASP.NET V if I so choose.  I'm sticking with my file dialog in my V and having the V update a data bound property in the VM.