0

With reference to What exactly are "WPF services"? and its attachment https://digitaltapestry.wordpress.com/2009/07/21/services-%E2%80%93-your-viewmodel-death-star/ .
Using services in WPF MVVM application, Can i use this type of service in ViewModel even to display MessageBox with Yes No Cancel buttons?
If yes, whitch data type should i return in ViewModel (Boolean Yes=> true No/Cancel => false) (MessageBoxResult)?

Gsk
  • 2,929
  • 5
  • 22
  • 29

1 Answers1

2

Can i use this type of service in ViewModel even to display MessageBox with Yes No Cancel buttons?

Yes.

If yes, whitch data type should i return in ViewModel (Boolean Yes=> true No/Cancel => false) (MessageBoxResult)?

The service should return a bool?, .e.g.:

public class DisplayMessageService : IDisplayMessage
{
    public bool? ShowDialog(string message)
    {
        MessageBoxResult result = MessageBox.Show(message, "title...", MessageBoxButton.YesNoCancel);
        switch (result)
        {
            case MessageBoxResult.Yes:
                return true;
            case MessageBoxResult.No:
                return false;
            default:
                return null;
        }
    }
}

If you simply display a MessageBox without any "Yes" or "No" button, you shouldn't return anything from the method.

mm8
  • 163,881
  • 10
  • 57
  • 88