I lately had the problem of creating add and edit dialogs for my wpf app.
All I want to do in my code was something like this. (I mostly use viewmodel first approach
This is a good approach and I used similar ones in the past. Go for it!
One minor thing I'd definitely do is make the event receive a boolean for when you need to set "false" in the DialogResult.
event EventHandler<RequestCloseEventArgs> RequestCloseDialog;
and the EventArgs class:
public class RequestCloseEventArgs : EventArgs
{
public RequestCloseEventArgs(bool dialogResult)
{
this.DialogResult = dialogResult;
}
public bool DialogResult { get; private set; }
}
I've been using an almost identical approach for several months now, and I'm very happy with it (i.e. I haven't yet felt the urge to rewrite it completely...)
In my implementation, I use a IDialogViewModel
that exposes things such as the title, the standad buttons to show (in order to have a consistent apparence across all dialogs), a RequestClose
event, and a few other things to be able to control the window size and behavior
If you are talking about dialogue windows and not just about the pop-up message boxes, please consider my approach below. The key points are:
Module Controller
into the constructor of each ViewModel
(you can use injection).Module Controller
has public/internal methods for creating dialogue windows (just creating, without returning a result). Hence to open a dialogue window in ViewModel
I write: controller.OpenDialogEntity(bla, bla...)
Pros:
Module Controller
is a simple way to avoid strong references and still allows to use mock-ups for testing.Cons:
<T>
where T
is enumeration of entities (or for simplicity it can be type of ViewModel).Module Controller
can be overwhelmed by methods for creating windows. In this case it's better to split it up in several modules.P.S. I have been using this approach for quite a long time now and ready to defend its eligibility in comments and provide some examples if required.