Close window MVVM style
I'm programming an application in WPF which uses the MVVM concept.
I'm trying to have no code-behind, and trying to keep my ViewModels and Views fully seperated. This means that I have to handle closing windows in xaml.
I want to execute a relaycommand, when I close the window, but only if I press "OK". If I press Cancel, the command must not be executed.
I have tried executing the relaycommand when the window on the trigger "IsClosing", but this doesn't work, because the command also is executed when "Cancel" is pressed. I know you can send the view as a parameter on relaycommand, but this means my ViewModel has knowledge of my View.
Does anyone know how to execute a relaycommand and close the window when I press "OK", without the ViewModel have any relation to the View?
Kind regards!
The best MVVM solution is to remove the window chrome (close button etc) and to provide explicit close and cancel buttons which you can bind to commands in the view-model.
To handle actually closing the window, use an attached behavior, as outlined in the accepted answer to this question - How should the ViewModel close the form?
链接地址: http://www.djcxy.com/p/56140.html上一篇: 按下WPF按键,修改XAML / MVVM中的点击命令
下一篇: 关闭窗口MVVM样式