A Behavior created by Haruhiro Isowa with assistance from Michael Washington.

Introduction

This article will show you how to use my PopUp Behavior that will launch a simple Yes/NoPopup and explain how it works so you will be able to tune it to your needs. This PopUp should make creating PopUps in MVVM / VM style easier by generating the PopUp for you which will save you the time of creating a View and ViewModel for a Popup, remove the need for complexity shown by other solutions that uses messaging / services and so forth. This also allows designers to have some control to create PopUps without waiting for the programmers to make a View and VM for them.

What HisowaSimplePopUpBehavior Do

This is the PopUp that it generates, but you can customize by modding the code or by properties on behavior.

These are the Properties. (All properties under Miscellaneous are bindable).

Brushes are to set your background of the Buttons and PopUp Window, gradients, images, etc. are ok.

CustomParameter: object, you pass to the behavior and when the PopUp closes it will return that to you. You can pass selected object or ID, etc.

PopUpMessage: string, set the message for the PopUp

PopUpNoMessage: string, set the text on the Nobutton

PopUpYesMessage: string, set the text on the Yesbutton

ReturnDialogResult: bool?, exposes the result of the PopUp (You can use two-way binding to pass back results. I used it to set IsChecked on a checkbox.

ReturnICommand: ICommand, The command to run when PopUp closes. It will pass in a dataholder class HisowaSimplePopUpBehaviorResult which will contain the DialogResult (bool?), DataContext of the Popup (Probably will not be used for this Popup Behavior ver), and InputParameter which is your CustomParameter you have set above.

By the properties of the behavior, you probably can guess what is going to happen. This Behavior will generate a simple Popup that you can set text, background values; pass in a parameter (probably useful to put the object in question); allow you to bind the DialogResult of the PopUp; and Most importantly run an ICommand when thePopUp Closes and passes you the DialogResult, DataContext, and the parameter you have passed so your ICommand can consume it.

Using my HisowaSimplePopUpBehavior

Start by opening a new Silverlight Application. Name it SimplePopUpBehaviorSample. Add my HisowaSimplePopUpBehavior class to your solution. Add a reference to System.Windows.Interactivity, and System.Windows.Controls. System.Windows.Interactivty is in Blend SDK. Build your project and open your MainPage.xaml in ExpressionBlend.

Before we can get started, we will need to have a View and ViewModel. Delete the MainPage.xamlin Blend. Add a new Item to the project by Right Clicking on the Project, select Add New Item. On the dialog, set User Control with ViewModel, name the file MainPage.xaml.

Open the MainPage.xaml.

Drag a button on the page. Then look for my behaviorHisowaSimplePopUpBehavior and drag it on the Button.

.

Now Build and run it from Blend. You should now have a simple Popup. At this point, it's not doing anything cool.

ViewModel Code

We need to setup some code in the ViewModel. But before we get started, we will use John Papa's DelegateCommand.cs file. Get it from Delegate Command From John Papa.

All this does is remove the handler, get the Icommand from the dependencyproperty, create my datacontainer HisowaSimplePopUpBehaviorResult, and run the Icommand passing the HisowaSimplePopUpBehaviorResult.

Lastly, the DataContainer HisowaSimplePopUpBehaviorResult class which is in the same file.

Comments and Discussions

How about building facility to pass a request Object to the page, show page in Modal mode and return a response object. I did it in ASP.NET 5 years ago by building Server Side control. IMHO This would be MUCH more useful for application development.

I don't really get what you mean, do you want to pass something and get it back? then its already there as CustomParameter.Do you want to pass in Popup window you have created? then I do have some solutions i am working on but not yet released.

I see, well I do have a prototype of something similar (you can inject any child window but VM has reference of it to set Result). This simple one was based off of that and slim-linned to be for a confirmation. Right now I'm working on finishing the behaviors that allows you to put any UIElement and set it as the content but the OK/Cancel will be made by the behavior. I also have a variation planned after that which will allow you fully inject your UI without my behavior making the buttons.Anyways those are planned to come soon.