如何在 wpf (mvvm) 中单击按钮时显示确定和取消的对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14064889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show a dialog with ok and cancel on button click in wpf (mvvm)?
提问by Virus
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?
我试图在单击按钮时显示一个带有确定和取消按钮的对话框。并且根据对话框结果,我想在 wpf 中保存\取消操作。我不确定如何在 wpf 中显示对话框。有什么帮助吗?
回答by Tilak
Have a look at Close View from View Modelarticle form code project
看看从视图模型关闭查看文章表单代码项目
回答by Mats Magnem
You don't use MVVM to do that. Using the MVVM pattern does notmean to neveruse events and codebehind.
你不使用 MVVM 来做到这一点。使用MVVM模式并不能意味着从未使用事件和代码隐藏。
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.
在我看来,使用 MVVM 模式的最佳组合是在 XAML 中使用绑定和命令以及其他“MVVM 内容”,以及事件和代码。如果它与 UI 严格有关(如消息框或双击或线程处理),请使用老式方式编写代码。
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.
所以使用 MessageBox.Show("hello"); 像往常一样在代码隐藏中,因为它与 UI 相关并且对模型/逻辑的意义较小。OpenFileDialog 和其他对话框也是如此。

