I created a simple user control using the Popup control as a starting point. The end result is a nice little user control that can be used as a popup control to display a message. It has only 1 button on it (an OK button that closes the popup control). I’ll outline the basic steps of creating the User Control and how the popup control works with it. Of course, you can create a popup control without the user control, but its a much cleaner practice to create user controls.
Step 1) Create a tester control. I called mine PopupTester.xaml. It can contain just about anything you want, in my case I put a funky looking green filled border on the control and a button, that when clicked will call the user control that we’re about to create. You can skip the code for the event handler for the button for now.
The PopupTester control is the default control that will be loaded when the Silverlight application is started. (Set this in App.xaml.cs)
Here is how the PopupTester control looks in Expression Blend. (It has no background color, which is why it appears gray.)
Step 2) Create a new User Control and name it MyPopupControl. The complete XAML for the control is shown a little further down. Remove the background, width, and height from the Layout Grid control so it only has a name property of LayoutRoot..
Step 3) Slap a Popup control in the main layout Grid. Name the popup control popMessage.
Step 4) Create a Grid inside of the Popup control. Create 3 rows and 3 columns in the Grid. Make the 2nd row and the 2nd column (the middle ones) have a Width = Auto. This creates a nice little grid that we can use to center the popup control contents. There are other ways to do this, but let’s swing with it.
Step 5) Add a Border to the Grid inside of the Popup control. Style the border however you wish, but make sure you give it a background color of some sort so it gets filled in. I gave mine a gradient background color from top to bottom and set the opacity to make it slightly transparent.
Step 6) Next, add a StackPanel inside of the Border, and stick a TextBlock and a Button inside of the Stackpanel. The TextBlock contain the message and the Button will be used to close the popup control. Name the TextBlock tbPopupMessage and the Button btnClose.
The XAML should look something like this:
Step 7) Now we need to ad some code to the MyPopupControl user control to create its event handlers. We’ll need events for the Button’s close event and to resize the control when the application resizes. So add the following constructor code to your code behind:
Using Visual Studio the event handlers are automatically created for the button. I used an anonymous delegate with lambda styling to for the resizing code just for fun. We could have simply created an event handler for it, though (like for the button). (Shawn WIldermuth inspired me to be creative with this technique after reading a post he wrote on creating a popup dialog, too. originally I had created an event handler for this, which is probably what I would od in a real application since I think its easier to debug & read.)
Step 8) Add the code to the button to close the popup control.
Step 9) Create a public property on the user control to get & set the Message in the control.
Step 10) Create a public method to open the control and one to close the control.
The code in the Show method sets the IsOpen property of the popup control, which makes the control open up. We also toggle Visibility. The Close method does the opposite.
Step 11) Go to the XAML of the PopupTester control and add the MyPopupControl user control as the last element just before the layout Grid closes.
Step 12) Once you add this XAML, you will have to add a reference to the namespace at the top of the user control, too, like this
Step 13) Go to the PopupTester control’s codebehind and add the following code.
This code adds the event handler for the button that will open the MyPopupControl instance. The event handler simply calls the popup control’s Show method, that we created in the previous step.
Step 14) Now compile the app and run it. When you click on the button it the popup control will appear to be in front of the entire PopupTester control (because of its gray slightly transparent and stretched background. Like this:
No magic, just pretty simple code and layouts.
Here is the source code …. Enjoy!