User prompts (MessageBox) with MVVM
- by mukapu
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
- Design my own dialog, then what to do from there
- 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...
- 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.
- Allow a property to be bound to tell when to show the MessageBox
- Provide command binding for possible user actions, Yes, No, Cancel...
How do I Use?
- Just place the necessary XAML tags in the view
- Implement the command for all user actions in the View Model
- Run unit tests on the commands