WPF MVVM dialog example WPF MVVM dialog example wpf wpf

WPF MVVM dialog example


I would use a service to display the dialog. The service can then also link views with viewmodels.

public interface IDialogService {    void RegisterView<TView, TViewModel>() where TViewModel:IDialogViewModel;    bool? ShowDialog(IDialogViewModel viewModel);}public interface IDialogViewModel {    bool CanClose();    void Close();}


RegisterView just links the view type with the ViewModel type. You can set up these links in the module initialization. This is simpler than trying to get modules to register datatemplates in the top layer of your application.

ShowDialog Shows the ViewModel you want to display. It returns true, false and null for close just like the Window.ShowDialog method. The implementation just creates a new view of type TView from your container, hooks it up to the provided ViewModel, and shows it.

IDialogViewModel provides a mechanism for the ViewModel to do verification and cancel the closing of the dialog.

I have a standard dialog window, with a content control in it. When ShowDialog is called it creates a new standard dialog, adds the view to the content control, hooks up the ViewModel and displays it. The standard dialog already has [OK] and [Cancel] buttons with the appropriate logic to call the right methods from IDialogViewModel.


The way I do this is using the mediator pattern also. When the ViewModel wants to show a dialog, it sends a message which is picked up by the application's main window. The message contains an instance of the ViewModel used by the dialog.

The main window then constructs an instance of the dialog window, passes the view model to it and shows the dialog. The result of the dialog is passed back to the caller in the original message.

It looks something like this:

In your view model:

DialogViewModel viewModel = new DialogViewModel(...);ShowDialogMessage message = new ShowDialogMessage(viewModel);_messenger.Broadcast(message);if (message.Result == true){    ...}

In the main window codebehind:

void RecieveShowDialogMessage(ShowDialogMessage message){    DialogWindow w = new DialogWindow();    w.DataContext = message.ViewModel;    message.Result = w.ShowDialog();}

I hope this is enough to give you the idea...


I'm agreed, that using service to display dialog according to MVVM pattern is the most simple solution. But, I also asked myself, if there are 3 assemblies in my project Model, ViewModel, View and according to MVVM pattern assembly ViewModel has a reference to Model, and View to both Model and ViewModel where should I place DialogService class? If I will place one in the ViewModel assembly - I have no chances to create DialogView instance; on the other hand, if I will place DialogService in the View assembly, how I should inject it in my ViewModel class?

So, I would recoment look at Advanced MVVM scenarios with Prism Part: Using Interaction Request Objects

As example of this approach:

DialogViewModelBase

public abstract class DialogViewModelBase : ViewModelBase{    private ICommand _ok;    public ICommand Ok    {        get { return _ok ?? (_ok = new DelegateCommand(OkExecute, CanOkExecute)); }    }    protected virtual bool CanOkExecute()    {        return true;    }    protected virtual void OkExecute()    {        _isSaved = true;        Close = true;    }    private ICommand _cancel;    public ICommand Cancel    {        get         {           return _cancel ?? (_cancel = new DelegateCommand(CancelExecute, CanCancelExecute));        }    }    protected virtual bool CanCancelExecute()    {        return true;    }    protected virtual void CancelExecute()    {        Close = true;    }    private bool _isSaved = false;    public bool IsSaved    {        get { return _isSaved; }    }    private bool _close = false;    public bool Close    {        get { return _close; }        set        {            _close = value;            RaisePropertyChanged(() => Close);        }    }}

CreateUserStoryViewModel:

public class CreateUserStoryViewModel : DialogViewModelBase{    private string _name = String.Empty;    public string Name    {        get { return _name; }        set        {            _name = value;            RaisePropertyChanged(() => Name);        }    }}

CreateUserStoryRequest

private InteractionRequest<Notification> _createUserStoryRequest;public InteractionRequest<Notification> CreateUserStoryRequest{    get    {        return _createUserStoryRequest ?? (_createUserStoryRequest = new InteractionRequest<Notification>());    }}

CreateUserStory Command

private void CreateUserStoryExecute(){    CreateUserStoryRequest.Raise(new Notification()    {        Content = new CreateUserStoryViewModel(),        Title = "Create User Story"    },     notification =>                 {                      CreateUserStoryViewModel createUserStoryViewModel =                               (CreateUserStoryViewModel)notification.Content;                      if (createUserStoryViewModel.IsSaved)                      {                         _domainContext.CreateUserStory(new UserStory(){ Name = createUserStoryViewModel.Name, });                      }                 });}

XAML:

<!--where xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"          xmlns:ir="clr-namespace:Microsoft.Practices.Prism.Interactivity.InteractionRequest;assembly=Microsoft.Practices.Prism.Interactivity"--><i:Interaction.Triggers>  <ir:InteractionRequestTrigger SourceObject="{Binding CreateUserStoryRequest}">    <ir:PopupChildWindowAction>      <ir:PopupChildWindowAction.ChildWindow>        <view:CreateUserStory />      </ir:PopupChildWindowAction.ChildWindow>    </ir:PopupChildWindowAction>  </ir:InteractionRequestTrigger></i:Interaction.Triggers>