Jun 3 2010

MVVM:Modal dialog before running Command

Category: Administrator @ 19:47

Drop this behavior on a button or any other UI Element to run a command when the button is clicked or the left mouse button is pressed.

The behavior accepts a Message and Caption and will optionally display a confirmation dialog before running the command.

This I very useful if you want to ask the user “are you sure” questions without having to display modal dialogs from the View Model.

In WPF you could use the Preview tunneling events to make it a bit nicer.

(Look mom, I made a messagebox. :-) Sometimes it's the how, not the what)

<Confirm:Confirm IsConfirm="{Binding ElementName=checkBoxConfirm, Path=IsChecked}" Command="{Binding Path=ButtonCommand}" CommandParameter="{Binding ElementName=textBoxParameter, Path=Text}" ConfirmMessage="Are you sure you want to fire the command?" ConfirmCaption="Question" ></Confirm:Confirm>

Download the source: ConfirmCommandBehavior.zip (38.69 kb)

Tags: , ,

Comments

1.
Web Development Web Development United States says:

Now a days Blogengine is going more popular as its quite good to use for blog sites, navigation provided  is so simple and easy and  because of this reason I inspired to write something  on the blog you have. And one more thing is that your content is too much informative.

2.
how to play roulette how to play roulette United States says:

A nice variety, more enrertaining than commom dialogue windows.

3.
bakeware bakeware United States says:

nice tips. thank you

4.
business list business list United States says:

Very nice article. You  encourage me to make my own page. Thanks for sharing your blog.

5.
pandora jewelry beads pandora jewelry beads United States says:

Hallo im from germany and my english isnt that awesome, but i was able to translate every single sentence of your post.

6.
Roulette System Roulette System Germany says:

Thanks for great info

7.
trip advisor trip advisor United States says:

Your effort has shown that you have worked very hard to make this valuable post indeed. Anyways thanks for this informative post to share.

8.
links of london charm bracelet links of london charm bracelet People's Republic of China says:

I really liked this article. This is so amazing, information about touring with a blind man make my knowledge increases, thank you!!

9.
tiffany on sale tiffany on sale United States says:

Tremendously educational many thanks, I reckon your trusty readers would definitely want even more writing like this carry on the excellent hard work.

10.
swarovski hotfix crystals swarovski hotfix crystals People's Republic of China says:

So informative things are provided here,I really happy to read this post,I was just imagine about it and you provided me the correct information I really bookmark it,for further reading,So thanks for sharing the information.
http://www.myjewelrybox.org/Earrings_7_2.htm
http://www.myjewelrybox.org/Earrings_7_3.htm

11.
Wristbands Wristbands United States says:

Hi. I treasured to drop you a quick note to verbalize my thanks. I’ve been observing your blog for a month or so and have plucked up a heap of fabulous information as well as enjoyed the way you’ve structured your site. I am seeking to run my own blog however I think its too general and I would like to focus more on smaller topics.

12.
Marc Gervais Marc Gervais Canada says:

Hello:

I came across this article in hopes that it would help me with a particular problem in my Silverlight application. However, I noticed that the ConfirmCommandBehavior does not allow for the Button to enabled/disabled according to the canExecute parameter of the RelayCommand<>().

You can confirm this simply by adding the following to the ViewModel.ButtonCommand property:
relayButtonCommand = new RelayCommand<Object>(ButtonAction, CanButtonAction);

and the following method to the ViewModel:
private bool CanButtonAction(Object item)
{
    return false;
}

You will see the Button is always enabled even though the code dictates it should be disabled.

I really like the idea of being able to do this confirmation without needing to place UI-like code in my ViewModel, but if I cannot have the ViewModel control whether or not the Command is available, what is the point?

Any ideas on how to still allow for the canExecute parameter to control the IsEnabled proeprty of the Button?

Thanks.

13.
Botox Botox United States says:

The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.