Although the View Model must not reference View classes, the View Model can still talk to the View. If there wasn't such a capability, then displaying the View Model's properties to the user would be impossible. The communication method I am referring to, of course, is data binding. Data binding is already the primary means of communication between the View and the View Model. We can use it for showing a dialog box as well.
Showing a dialog box using the solution I have created involves two easy steps:
- Add the
MessageBoxHelper.Messageattached property into the View.
- Within the View Model, set the property specified in the first step when you want the dialog box to be shown.
DialogPM. This class contains no View-specific logic. It's purpose, rather, is to transport parameters for the
MessageBox.Show(...method call from the View Model to the View and, if that method's result is important, back from the View to the View Model.
Download the Qnomad CoreHelpers library to see the complete example.