Have a look at Close View from View Model article form code project
How to show a dialog with ok and cancel on button click in wpf (mvvm)?
-
12-12-2021 - |
题
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?
解决方案
其他提示
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.