WPF OpenFileDialog with the MVVM pattern?

Viewed 71390

I just started learning the MVVM pattern for WPF. I hit a wall: what do you do when you need to show an OpenFileDialog?

Here's an example UI I'm trying to use it on:

alt text

When the browse button is clicked, an OpenFileDialog should be shown. When the user selects a file from the OpenFileDialog, the file path should be displayed in the textbox.

How can I do this with MVVM?

Update: How can I do this with MVVM and make it unit test-able? The solution below doesn't work for unit testing.

5 Answers

From my perspective the best option is the prism library and InteractionRequests. The action to open the dialog remains within the xaml and gets triggered from Viewmodel while the Viewmodel does not need to know anything about the view.

See also

https://plainionist.github.io///Mvvm-Dialogs/

As example see:

https://github.com/plainionist/Plainion.Prism/blob/master/src/Plainion.Prism/Interactivity/PopupCommonDialogAction.cs

https://github.com/plainionist/Plainion.Prism/blob/master/src/Plainion.Prism/Interactivity/InteractionRequest/OpenFileDialogNotification.cs

Related