Cancel command on button click

喜欢而已 提交于 2019-12-25 02:28:07

问题


I currently have a windows phone 8 application that has a user control that is bound to some view model. In the view model I a have a command property that binds to a button on the view.

I want to popup a message box in the view that asks "Are you sure you want to do X?". If Yes it needs to fire the command that is bound to the button else it needs to not fire the command.

I obviously do not want to fire the MessageBox from the view model since this is view logic. So how do I show the MessageBox in the view and then not fire the command that it is bound to it.

Edit: Let me just make it clear. The command is in the viewmodel and bound in xaml. The MessageBox show is in the view code behind (in the buttons's click event). I would like to keep it like this. My question is just. How do I not fire the command once the botton is pressed and the MessageBox returns Cancel.


回答1:


You can do it on click event of the button. you can popup the message box in click event handler. If user wants to cancel the operation than simply set the e.handle = true.

If you don't want to use code-behind, you can create your own custom button where you can override OnClick() method of the button. In the method show the message box and if user wants to cancel the event, just don't call base.OnClick();

You can refer following link where author has explained that how you can implement cancellable commands:

http://www.codeproject.com/Articles/274982/Commands-in-MVVM#hdiw1




回答2:


Ok so I have found a solution.

I created a new UserControl: ConfirmationButton with a dependency ICommand property for if the confirmation passed. In the control I manually fire the command if the MessagBox Returns Yes. I then use this new UserControl where I need such functionality.



来源:https://stackoverflow.com/questions/21113730/cancel-command-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!