单击按钮时取消命令 [英] Cancel command on button click

查看:35
本文介绍了单击按钮时取消命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 Windows Phone 8 应用程序,它有一个绑定到某个视图模型的用户控件.在视图模型中,我有一个命令属性绑定到视图上的按钮.

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.

我想在视图中弹出一个消息框,询问您确定要执行 X 吗?".如果是,它需要触发绑定到按钮的命令,否则它需要不触发命令.

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.

我显然不想从视图模型中触发 MessageBox,因为这是视图逻辑.那么如何在视图中显示 MessageBox,然后不触发它绑定到它的命令.

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.

让我说清楚.该命令在视图模型中并绑定在 xaml 中.MessageBox 显示在后面的视图代码中(在按钮的单击事件中).我想保持这样.我的问题只是.一旦按下按钮并且 MessageBox 返回取消,我如何不触发命令.

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.

推荐答案

好的,我找到了解决方案.

Ok so I have found a solution.

我创建了一个新的 UserControl: ConfirmationButton,它带有一个依赖 ICommand 属性,用于确认是否通过.在控件中,如果 MessagBox 返回 Yes,我会手动触发命令.然后我在需要此类功能的地方使用这个新的 UserControl.

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.

这篇关于单击按钮时取消命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆