Open file dialog

Topics: Prism v4 - WPF 4
Apr 27, 2014 at 10:04 PM

How should I handle an open file dialog? I have created a view that contains a textbox and a button. When the user presses the button, open file dialog is shown. When the user selects a file, the textbox is filled with the full path of the file.

Should I place code in the ViewModel or put some logic code behind?

Apr 28, 2014 at 7:12 PM

I am afraid I did not get the concrete problem you are asking about. Would like to know how to implement the complete workflow from pressing the button to have the textbox filled automatically? Or do you just need to decide where is the best place to implement that behavior?

Based on the MVVM pattern, any logic would be implemented on the ViewModel class, which it would also be tested without the need of instantiating the View class. From the Prism Guide documentation:
The view model implements properties and commands to which the view can data bind and notifies the view of any state changes through change notification events. The properties and commands that the view model provides define the functionality to be offered by the UI, but the view determines how that functionality is to be rendered.

In addition, you may find a similar popup communication on the Interactivity QuickStart, where the result returned from the popup is gathered in the InteractionRequestViewModel and then notified.

Gabriel Ostrowsky
Apr 28, 2014 at 8:34 PM
Hi Gabriel,

Thank you very much for your reply.

I decided to implement the code, that sets the text of the textbox equal to the selected file, in code behind according to this from the Prism Guide documentation:

The view's responsibility is to define the structure and appearance of what the user sees on the screen. Ideally, the code-behind of a view contains only a constructor that calls the InitializeComponent method. In some cases, the code-behind may contain UI logic code that implements visual behavior that is difficult or inefficient to express in Extensible Application Markup Language (XAML), such as complex animations, or when the code needs to directly manipulate visual elements that are part of the view.

With this approach, my ViewModel will be full testable without the construction of the View, since I have created a Command to the ViewModel and assigned it to the button that will process the selected file.