How have you successfully implemented MessageBox.Show() functionality in MVVM?

I've got a WPF application which calls MessageBox.Show() way back in the ViewModel (to check if the user really wants to delete). This actually works, but goes against the grain of MVVM since the ViewModel should not explicitly determine what happens on the View.

So now I am thinking how can I best implement the MessageBox.Show() functionality in my MVVM application, options:

  1. I could have a message with the text "Are you sure...?" along with two buttons Yes and No all in a Border in my XAML, and create a trigger on the template so that it is collapsed/visible based on a ViewModelProperty called AreYourSureDialogueBoxIsVisible, and then when I need this dialogue box, assign AreYourSureDialogueBoxIsVisible to "true", and also handle the two buttons via DelegateCommand back in my ViewModel.

  2. I could also somehow try to handle this with triggers in XAML so that the Delete button actually just makes some Border element appear with the message and buttons in it, and the Yes button did the actually deleting.

Both solutions seem to be too complex for what used to be a couple lines of code with MessageBox.Show().

In what ways have you successfully implemented Dialogue Boxes in your MVVM applications?

Of the two you mention, I prefer option #2. The Delete button on the page just makes the "Confirm Delete Dialog" appear. The "Confirm Delete Dialog" actually kicks off the Delete.

Have you checked out Karl Shifflett's WPF Line Of Business Slides and Demos? I know he does something like this. I'll try to remember where.

EDIT: Check out Demo #11 "Data Validation in MVVM" (EditContactItemsControlSelectionViewModel.DeleteCommand). Karl calls a popup from the ViewModal (What!? :-). I actually like your idea better. Seems easier to Unit Test.

How have you successfully implemented MessageBox.Show(), Show() way back in the ViewModel (to check if the user really wants to delete). This actually works, … How have you successfully implemented MessageBox.​Show() functionality in MVVM Show() functionality in MVVM? stackoverflow.​com. To use it in a View, which will cause it to actually show a MessageBox, you just subscribe to the event and call e.Show() in the event handler, like this: And that is all you need to do to show MVVM friendly Windows MessageBoxes.

Services to the rescue. Using Onyx (disclaimer, I'm the author) this is as easy as:

public void Foo()
    IDisplayMessage dm = this.View.GetService<IDisplayMessage>();
    dm.Show("Hello, world!");

In a running application, this will indirectly call MessageBox.Show("Hello, world!"). When testing, the IDisplayMessage service can be mocked and provided to the ViewModel to do what ever you want to accomplish during the test.

Foundation Expression Blend 3 with Silverlight, So now I am thinking how can I best implement the MessageBox.Show() functionality in my MVVM application, options: I could have a message with the text "Are  How have you successfully implemented MessageBox.Show() functionality in MVVM? WPF MVVM – Simple ‘MessageBox.Show’ With Action & Func by Dean Chalk this answer edited Feb 17 '16 at 18:39 honk 3,775 11 28 48 answered Sep 14 '10 at 4:29 Nathiya 2,450 17 58 117 1 Yes, Dean Chalk's post is definitely one of the simplest and most elegant

To expand on Dean Chalk's answer now that his link is kaput:

In the App.xaml.cs file we hook up the confirm dialog to the viewmodel.

protected override void OnStartup(StartupEventArgs e)
    var confirm = (Func<string, string, bool>)((msg, capt) => MessageBox.Show(msg, capt, MessageBoxButton.YesNo) == MessageBoxResult.Yes);
    var window = new MainWindowView();
    var viewModel = new MainWindowViewModel(confirm);
    window.DataContext = viewModel;

In the view (MainWindowView.xaml) we have a button that calls a command in the ViewModel

<Button Command="{Binding Path=DeleteCommand}" />

The viewmodel (MainWindowViewModel.cs) uses a delegate command to show the "Are you sure?" dialog and perform the action. In this example it is a SimpleCommand similar to this, but any implementation of ICommand should do.

private readonly Func<string, string, bool> _confirm;

public MainWindowViewModel(Func<string, string, bool> confirm)
    _confirm = confirm;

#region Delete Command
private SimpleCommand _deleteCommand;
public ICommand DeleteCommand
    get { return _deleteCommand ?? (_deleteCommand = new SimpleCommand(ExecuteDeleteCommand, CanExecuteDeleteCommand)); }

public bool CanExecuteDeleteCommand()
    //put your logic here whether to allow deletes
    return true;

public void ExecuteDeleteCommand()
    bool doDelete =_confirm("Are you sure?", "Confirm Delete");
    if (doDelete)
        //delete from database

c# - WPF MessageBox with MVVM pattern?, And there you have it, a very good implementation of the MVVM: Our View is, in fact, very dumb in This MessageBox run. The MVVM then runs code that provides the functionality. shows us that our ShowMsgBox Command is firing correctly. Thank you bro for your great article, and I have a question. what if the programmer of the UI doesn't want to use this messagebox, what if the programmer wants a window full of content then the OK CANCEL buttons, sometimes there will be no need for a MessageBox and many articles were discussing this in the MVVM context, My approach is to implement an event and then let the UI use our event or

What about raising an Event like "MessageBoxRequested" handled in the codebehind of the View (anyway it's View only code so I don't see any problem with having this code on the codebehind).

Pro Business Applications with Silverlight 5, Have an interface IMessageBoxService as: ShowMessage to show the MessageBox. You could bind your messagebox control's visibility to the validation. 1) This violates SRP. You're making view model responsible for everything. 2) Instead of 2 service implementations (one for production, one for testing), you should keep 2 implementations of each view model, which requires this functional.

I just create an interface (IMessageDisplay or similar) which gets injected into the VM, and it has methods like a MessageBox (ShowMessage() etc). You can implement that using a standard messagebox, or something more WPF specific (I use this one on CodePlex by Prajeesh).

That way everything's separated and testable.

Advanced MVVM Scenarios Using the Prism Library for WPF, When you click the button, the login operation exposed by the ViewModel will be called, via action when the event is raised, showing the message box. of additional other features) that you may wish to consider: • MVVM Light Toolkit: your own and that it's not necessary or worthwhile to implement a major framework. I have a Windows Forms application VS2010 C# where I display a MessageBox for show a message. I have an okay button, but if they walk away, I want to timeout and close the message box after lets say

MVVM, The section, Commands in Implementing the MVVM Pattern, described how commands can In some cases, you will want the commands for all shown views to be You can use this functionality to implement the example described earlier. For example, in WPF, the MessageBox class can be used to implement a truly  The purpose of this post is to provide an introduction to the Model-View-ViewModel (MVVM) pattern. While I’ve participated in lots of discussions online about MVVM, it occurred to me that beginners who are learning the pattern have very little to go on and a lot of conflicting resources to wade through in order to try to implement it in their own code.

MessageBox Class (System.Windows.Forms), When implementing the MVVM pattern, we have a separation between the Figure 1 shows two-way data binding between a XAML view and its In WPF, which is the most advanced XAML platform available in terms of number of features, a designer would create a custom message box, which would be implemented  If you are serious about implementing the MVVM pattern in your UI applications you should be well aware of the fact that any call you make to System.Windows.MessageBox.Show from your view models violates this pattern and the separation of concerns that exists between the application's logic and its presentation.

Recommendations and best practices for implementing MVVM and , user closes it. A MessageBox can contain text, buttons, and symbols that inform and instruct the user. MessageBox.Show method. string message = "You did not enter a server name. Serves as the default hash function. (Inherited ToString(). Returns a string that represents the current object. (Inherited from Object)  You likely also encountered issues implementing some functionality under MVVM. Some mechanisms are difficult to implement without moving away from MVVM. What’s more, MVVM limitations may not relate to any particular control because the WPF/Silverlight platform itself has no full support for MVVM development.