Caliburn Micro: DialogResult
Asked Answered
M

3

5

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?

Mariellemariellen answered 15/5, 2012 at 9:11 Comment(0)
R
9

I tend to use the View Model to handle determining what happened in the dialog. For instance, you can have an IsCancelled property on your UserViewModel that you can interrogate after returning from the ShowDialog call. Something like:

public void UserNew() {
    var userViewModel = new UserViewModel(this._windowManager);
    this._windowManager.ShowDialog(userViewModel);
    if (userViewModel.IsCancelled) {
        // Handle cancellation
    } else {
        // Handle other case(s)
    }
}
Reinstate answered 15/5, 2012 at 16:7 Comment(1)
see my answer, where you only have to inherit from Screen and don't have to add a property yourself to the vm.Everywhere
E
10

In caliburn micro in your dialogviewmodel which inherits from Screen you can do:

TryClose(true); // for OK

or

TryClose(false); // for Cancel

then you could do:

var vm = IoC.Get<MyViewModel>();
var r = WindowManager.ShowDialog(vm, null, null);

if (r.HasValue && r.Value) {
  // do something on OK
}

your xaml of the dialog might look like this:

<Button Content="OK" cal:Message.Attach="[Event Click] = [AcceptButton()]" />
<Button Content="Cancel" cal:Message.Attach="[Event Click] = [CancelButton()]" />

using this namespace:

xmlns:cal="http://www.caliburnproject.org"

This is a detailed code sample of the dialog viewmodel implementation:

public bool CanAcceptButton
{
  get { return true; /* add logic here */ }
}

public void AcceptButton()
{
  TryClose(true);
}

public bool CanCancelButton
{
  get { return true; }
}

public void CancelButton()
{
  TryClose(false);
}
Everywhere answered 10/10, 2013 at 10:26 Comment(1)
instead of "var r = WindowManager..." change that to "bool? r = WindowManager..." because if you return TryClose(false) then Visual Studio 2010 makes it a boolean and crashes on the if (r.HasValue) because a bool doesn't have .HasValue.Everywhere
R
9

I tend to use the View Model to handle determining what happened in the dialog. For instance, you can have an IsCancelled property on your UserViewModel that you can interrogate after returning from the ShowDialog call. Something like:

public void UserNew() {
    var userViewModel = new UserViewModel(this._windowManager);
    this._windowManager.ShowDialog(userViewModel);
    if (userViewModel.IsCancelled) {
        // Handle cancellation
    } else {
        // Handle other case(s)
    }
}
Reinstate answered 15/5, 2012 at 16:7 Comment(1)
see my answer, where you only have to inherit from Screen and don't have to add a property yourself to the vm.Everywhere
A
0

WPF dialogs return nullable bools instead of DialogResults. Caliburn's ShowDialog also returns bool?

From MSDN

Dialog boxes commonly allow users to accept or cancel the task for which they were shown before the dialog box is closed. ShowDialog returns a Nullable Boolean value that specifies whether the activity was accepted or canceled. The return value is the value of the DialogResult property before a window closes. For more information, see DialogResult.

DialogResult above refers to the bool property called DialogResult on System.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.

Ashur answered 15/5, 2012 at 9:27 Comment(2)
How can I set the value of the nullable boolean? Currently I close the dialog with (GetView() as Window).Close() from the dialog view model.Mariellemariellen
I found also a solution to set the bool return value in the UserViewModel: var window = GetView() as Window; window.DialogResult = false; window.Close();Mariellemariellen

© 2022 - 2024 — McMap. All rights reserved.