0

I am trying to display a dialog with ok and cancel button on a button click. And depending on the dialog result I would like to save\cancel the operation in wpf. I am not sure how to show a dialog in wpf. Any help?

Virus
  • 3,215
  • 7
  • 29
  • 46

2 Answers2

2

Have a look at Close View from View Model article form code project

Showing Dialogs When Using the MVVM Pattern

MVVM and DialogResult

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • thanks! But is there any default show dialog box available in WPF? or we have to create our own dialog? – Virus Dec 28 '12 at 05:20
  • Do you require MessageBox, or custom dialog box? – Tilak Dec 28 '12 at 05:21
  • I think as of now a MessageBox would be enough as I have to confirm the operation by showing a message. If user clicks on cancel then cancel the operation else save the operation. – Virus Dec 28 '12 at 05:23
  • [System.Windows.MessageBox](http://msdn.microsoft.com/en-us/library/ms602949.aspx) is WPF Messagebox equivalent. You should wrap messagebox over IMesageBoxService, and in your implementation you should map DialogBoxResult to yes/no etc. – Tilak Dec 28 '12 at 05:23
1

You don't use MVVM to do that. Using the MVVM pattern does not mean to never use events and codebehind.

In my opinion, the best healthy mix using the MVVM pattern is to use bindings and commands and other "MVVM-stuff" in the XAML, but also events and code. If it has to do with the UI strictly (like a messagebox or double click or thread handling), use code the old-fashion way.

So use the MessageBox.Show("hello"); in the codebehind as usual, because it is UI related and has less meaning to the model/logic. The same goes with OpenFileDialog and other dialogs.

Mats Magnem
  • 1,375
  • 1
  • 10
  • 21