Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

c# - Cancel command on button click

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...