Open File Dialog MVVM

mvvm, openfiledialog, wpf

Solution

The best thing to do here is use a service.

A service is just a class that you access from a central repository of services, often an IOC container. The service then implements what you need like the OpenFileDialog.

So, assuming you have an `IFileDialogService` in a Unity container, you could do...

void Browse(object param)
{
    var fileDialogService = container.Resolve<IFileDialogService>();

    string path = fileDialogService.OpenFileDialog();

    if (!string.IsNullOrEmpty(path))
    {
        //Do stuff
    }
}

Problem

Ok I really would like to know how expert MVVM developers handle an openfile dialog in WPF. I don't really want to do this in my ViewModel(where 'Browse' is referenced via a DelegateCommand) ``` void Browse(object param) { //Add code here OpenFileDialog d = new OpenFileDialog(); if (d.ShowDialog() == true) { //Do stuff } } ``` Because I believe that goes against MVVM methodology. What do I do?

Original source

Related problems