User prompts (MessageBox) with MVVM

The problem statement:

I am tired of thinking how to show a simple message box or user prompt and act based on the response in Model-View-View-Model (MVVM).

Common approaches:

- It's ok, let's just do this one thing from ViewModel and mock this out for unit testing!(https://geekswithblogs.net/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/wink_smile.gif)

- Design my own dialog, then what to do from there!(https://geekswithblogs.net/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/devil_smile.gif)

- Can I write something in view code behind, ah yes, that seems to be the only way out, as anyway MVVM is still not matured... !(https://geekswithblogs.net/Providers/BlogEntryEditor/FCKeditor/editor/images/smiley/msn/whatchutalkingabout_smile.gif)

- and what not?

I am pretty much one among the few frustrated out in this world looking for some convincing answers. I think we can do it a little neater without having the feeling of violating any of our self defined rules!

Solution:

The Control

- Implement a simple control with no designer visibility.

!(/images/mukapu/user-prompts-messagebox-with-mvvm/70b54ed8-UserPrompt1.png)

- Allow a property to be bound to tell when to show the MessageBox

!(/images/mukapu/user-prompts-messagebox-with-mvvm/504a2e6d-UserPrompt2.png)

!(/images/mukapu/user-prompts-messagebox-with-mvvm/30006bbb-UserPrompt3.png)

- Provide command binding for possible user actions, Yes, No, Cancel...

!(/images/mukapu/user-prompts-messagebox-with-mvvm/5853efb6-UserPrompt4.png)

How do I Use?

- Just place the necessary XAML tag in the view

!(/images/mukapu/user-prompts-messagebox-with-mvvm/998c0712-UserPrompt5.png)

- Implement the command for all user actions in the View Model

!(/images/mukapu/user-prompts-messagebox-with-mvvm/1c8c410e-UserPrompt6.png)

- Run unit tests on the commands

!(/images/mukapu/user-prompts-messagebox-with-mvvm/b4af0bc2-UserPrompt7.png)

I am also attaching the code for the MessageBox control and a demo application.

Hope this is useful to somebody out there... Comments are welcome....

!(file:///C:/DOCUME1/706015/LOCALS1/Temp/moz-screenshot-1.png)

This article is part of the GWB Archives. Original Author: mukapu

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.