Often when building Silverlight RIA you need to prompt the user to confirm an action; Say you are deleting an item you want to make sure they approve the deletion. Just like you do in Windows/Mac OS. In Silverlight 3 the ChildWindow control was added which basically popups a dialog window where you can focus the users attention until you get a confirmation or cancelation on the matter at hand.
In my example I have placed a button inside a StackPanel which once click Shows my ChildWindow. What I do is create the ChildWindow on MainPage Loaded event as a private member and then I can just show or hide it. The reason I do not just keep creating new instances of the ChildWindow is because the garbage collection in Silverlight isn’t the greatest and I would prefer to keep my apps to a minimum.

Silverlight ChildWindow Example

Silverlight ChildWindow in action
This is how I setup my ChildWindow as a private member on MainPage.xaml.cs:
private PopupWindow _popupWindow;
This is how I setup my ChildWindow instance (line 6) on the MainPage Loaded Event:
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
SilverlightForums.Click += new RoutedEventHandler(SilverlightForums_Click);
SilverlightTutorials.Click += new RoutedEventHandler(SilverlightTutorials_Click);
// Create instance of PopupWindow
_popupWindow = new PopupWindow();
// Hookup Click Event
ShowPopup.Click += new RoutedEventHandler(ShowPopup_Click);
}
When I click my button on MainPage it calls this Click() method:
private void ShowPopup_Click(object sender, RoutedEventArgs e)
{
// Populate PopupWindow
_popupWindow.ErrorTitle = "If you have any questions visit SilverlightForums.com and ask
";
// Display PopupWindow
_popupWindow.DisplayPopup();
}
On my ChildWindow (called PopupWindow) I have created a property that I can pass through a string message:
#region Properties
/// <summary>
/// Gets or sets the title
/// </summary>
public string ErrorTitle
{
get { return _errorTitle; }
set { _errorTitle = value; }
}
#endregion
To Show and Hide the ChildWindow I have two public methods. One to Show it and one to hide it.
#region Public methods
public void DisplayPopup()
{
this.Show();
}
public void HidePopup()
{
this.Close();
}
#endregion
Here is the Xaml From the ChildWindow to show you what it looks like:
<controls:ChildWindow x:Class="SilverlightForums_ChildWindow.PopupWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Width="250" Height="200"
Title="PopupWindow">
<StackPanel x:Name="LayoutRoot" Orientation="Vertical">
<TextBlock x:Name="Title" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextWrapping="Wrap" />
<StackPanel Orientation="Horizontal" Margin="30,50,0,0">
<Button x:Name="OK" Content="OK" Width="75" Height="23" HorizontalAlignment="Right" />
<Button x:Name="Cancel" Content="Cancel" Width="75" Height="23" HorizontalAlignment="Left" />
</StackPanel>
</StackPanel>
</controls:ChildWindow>
If you download the example you will see that the source code is quite easy to follow (probably easier to follow than this posts actually
). If you have any issues or questions feel free to discuss them here
Thank you to Silverlightfreak at Silverlight Forums who requested this Silverlight tutorial. We hope you find it useful

