Framework simplifying the concept of opening dialogs from a view model when using MVVM in WPF or UWP.
Clone or download

README.md

MVVM Dialogs logo

MVVM Dialogs Build status NuGet NuGet

Table of contents


Introduction

MVVM Dialogs is a framework simplifying the concept of opening dialogs from a view model when using MVVM in WPF (Windows Presentation Framework) or UWP (Universal Windows Platform). It enables the developer to easily write unit tests for view models in the same manner unit tests are written for other classes.

The framework has built in support for the following dialogs in WPF:

  • Modal window
  • Non-modal window
  • Message box
  • Open file dialog
  • Save file dialog
  • Folder browser dialog

The framework has built in support for the following dialogs in UWP:

  • Content dialog
  • Message dialog
  • Single file picker
  • Multiple files picker
  • Save file picker
  • Single folder picker

WPF usage

To open a modal window, decorate the view with the attached property DialogServiceViews.IsRegistered:

<UserControl
    x:Class="DemoApplication.Features.Dialog.Modal.Views.ModalDialogTabContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:md="https://github.com/fantasticfiasco/mvvm-dialogs"
    md:DialogServiceViews.IsRegistered="True">

  ...

</UserControl>

With the view registered the view model is now capable of opening a dialog using the interface IDialogService:

public class ModalDialogTabContentViewModel : INotifyPropertyChanged
{
    private readonly IDialogService dialogService;

    public ModalDialogTabContentViewModel(IDialogService dialogService)
    {
        this.dialogService = dialogService;
    }

    ...

    private void ShowDialog()
    {
        var dialogViewModel = new AddTextDialogViewModel();

        bool? success = dialogService.ShowDialog(this, dialogViewModel);
        if (success == true)
        {
            Texts.Add(dialogViewModel.Text);
        }
    }
}

UWP usage

With UWP you don't need to register the view, simply open the dialog using the interface IDialogService:

public class MainPageViewModel : INotifyPropertyChanged
{
    private readonly IDialogService dialogService;

    public MainPageViewModel(IDialogService dialogService)
    {
        this.dialogService = dialogService;
    }

    ...

    private async void ShowContentDialog()
    {
        var dialogViewModel = new AddTextContentDialogViewModel();

        ContentDialogResult result = await dialogService.ShowContentDialogAsync(dialogViewModel);
        if (result == ContentDialogResult.Primary)
        {
            Texts.Add(dialogViewModel.Text);
        }
    }
}

Custom windows

Dialogs in WPF that don't inherit from Window, or content dialogs in UWP that don't inherit from ContentDialog are called custom dialogs. These custom dialogs are supported, but in order for DialogService to know how to interact with them, you will have to implement the IWindow interface in WPF or IContentDialog in UWP.

Custom framework dialogs

MVVM Dialogs is by default opening the standard Windows message box or the open file dialog when asked to. This can however be changed by providing your own implementation of IFrameworkDialogFactory to DialogService.

More in the wiki

For more information regarding the concepts of MVVM Dialogs and extended samples on the subjects, please read the wiki.

MVVM Dialogs Contrib

The world is full of snowflakes and all applications are unique in some way. MVVM Dialogs takes no claim to solve all issues regarding dialogs, but is a fantastic solution for most applications. The rest, the applications deviating from the common path, may require specific changes to the behavior of MVVM Dialog. For those there is MVVM Dialogs Contrib. A repository with features and functionality developed by the open source community, solving very specific needs.

If MVVM Dialogs for some reason doesn't fit your application, raise an issue in MVVM Dialogs Contrib and we'll see what we can do. These features are always implemented by the community, but shepherd by the maintainers of MVVM Dialogs.

Install MVVM Dialogs via NuGet

If you want to include MVVM Dialogs in your project, you can install it directly from NuGet.

To install MVVM Dialogs, run the following command in the Package Manager Console:

PM> Install-Package MvvmDialogs

History

MVVM Dialogs started out as an article on CodeProject with its first revision published in May 2009. At that time MVVM was still new and fresh, and the now hugely popular MVVM Light had yet not been published. In fact, none of the MVVM libraries commonly used today existed back then. MVVM Dialogs came about out of necessity to work with dialogs from the view model, a reaction to the fact that although MVVM was a popular pattern, the support to implement it was rather limited.

Now many years later, MVVM Dialogs is still maintained and new features are implemented on demand. The framework has grown up, and found its place in the world...

Reputation

MVVM Dialogs has earned enough reputation to be listed on Awesome .NET!, in company with other awesome MVVM libraries.

Credit

Thank you JetBrains for your important initiative to support the open source community with free licenses to your products.

JetBrains