Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caliburn Micro: DialogResult

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?

like image 658
Timm Bremus Avatar asked May 15 '12 09:05

Timm Bremus


2 Answers

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);
}
like image 51
juFo Avatar answered Nov 19 '22 18:11

juFo


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)
    }
}
like image 34
Megan Avatar answered Nov 19 '22 19:11

Megan