I've been through too much pain trying to switch between managing VM state for various views in the shell window, together with VM state for numerous edit dialogues, modal or non-modal, and state for the windows themselves.
Now I wish to redo with one shell window, and hoping for only one dialogue window, then my windows and Vms have far less coupling, and I can borrow the shell's VM management patterns into the dialogue's management.
I see quite a lot of guidance for directly managing popups and less generalised non-modals, but is there any established pattern or guidance for using only one dialogue to host various VMs? Even better, is there any such functionality available in Prism already?
I want to really to stick the DRY principle, and inheritance in both views and VMs is adding complexities I can't afford on a supposed to be quick first iteration prototype.
Well I don't know about any out of the box solution, but creating such a reusable dialog implementation is not that hard. Actually, I implemented something like this a few years ago. Well it was in another job, so I don't have access to the code anymore. Additionally, I cannot remember all the details, but I can try to give you the basic idea of such an implementation.
You can create a DialogVm
, which is providing the generic dialog functionality.
First of all, what is needed for a dialog in presentation layer? Usually, three buttons, like...
So as you can see, you need three commands (ICommand
doc). Actually, I've created a DelegateCommand
implementation (based on this). ICommand.CanExecute
determines, whether a bound button is disabled or enabled. If a command is null, the button should be hidden.
(If you're using the right layout control, the positions of the buttons are adjusted properly, if a button is not shown.)
To provide support for more than the four scenarios above, I added a CommandTitle
property to DelegateCommand
, so that the content of the button is coming from there.
Next thing you'll need a Title
property for the title of the dialog. So add this to the DialogVm
.
If you want to be able to close the dialog (only necessary, if it's a child of Window
) by executing a command, you can follow this approach. Of course I've used the version, which I've described there. But the others are also looking promising.
The last open point is a property, which represents the different dialog contents. If I recall it correctly, I've used a small set of view models and corresponding DataTemplates
(and of course a TemplateSelector
, which is providing the right template based on the VM type). Of course you will also need a ContentPresenter
control in your dialog, which is showing the DataTemplate
provided by the TemplateSelector
.
This is the only downsite, it's only a good approach, if you have only a few different dialog types (e.g. Question box, Message box...)
The usage is pretty easy. Simply, initialize a DialogVm
instance with the desired ICommand
logic, the DialogContentVm (however you want to call it), pass it to the DialogWindow (perhaps, you want to use sth. different e.g. a flyout) and present it to the user.
Well I hope it helps. If you need more info or any help, pls let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With