I can't find a solution for the following problem:
I open a Dialog with the WindowManager from caliburn micro:
public void UserNew()
{
this._windowManager.ShowDialog(new UserViewModel(this._windowManager));
}
Now I need a DialogResult when the user close the dialog with the OK button. The ShowDialog method of WindowManager don't return a DialogResult...
Can anyone help me?
In caliburn micro in your dialogviewmodel which inherits from Screen you can do:
or
then you could do:
your xaml of the dialog might look like this:
using this namespace:
This is a detailed code sample of the dialog viewmodel implementation:
I tend to use the View Model to handle determining what happened in the dialog. For instance, you can have an
IsCancelled
property on yourUserViewModel
that you can interrogate after returning from theShowDialog
call. Something like:WPF dialogs return nullable bools instead of DialogResults. Caliburn's ShowDialog also returns
bool?
From MSDN
DialogResult
above refers to thebool
property called DialogResult onSystem.Windows.Window
.If you want to return something more complex just define your own enum property on your window and read its value once the dialog has closed.
You can set DialogResult by using button click events in the view. This will be returned by Caliburn Micro's
WindowManager.ShowDialog()
method.In the caller codebehind:
In the dialog codebehind:
In the dialog XAML file:
In the ViewModel:
I've verified that the
Ok()
andCancel()
calls are made, that theOk_OnClick()
Cancel_OnClick()
events are fired, and that the return value fromIWindowManager.ShowDialog()
is correct.