ChildWindow in Silverlight3

Here we show how to make pop-up alert windows in Silverlight 3 and show how we get information back about what buttons where pressed, etc. in the child window. This provides a window that can be displayed over a parent window and blocks interaction with the parent window.

Create a normal Silverlight Application. Add a new item to the project of the type Silverlight Child Window type template.

Full source code for MainPage.xaml

   <UserControl x:Class="ChildWindowDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="600" Height="500">
    <Grid x:Name="LayoutRoot" Background="Bisque">
        <Button Content="Show Popup" HorizontalAlignment="Center"
            VerticalAlignment="Center" Width="300"
            FontSize="18" x:Name="btnShowPopUp"
            Height="50"  />
        <TextBlock x:Name="txtStatus" Width="400" Height="50"
                   Margin="80,150,0,0"
                   FontSize="18"/>
    </Grid>
</UserControl>

   

Full Source code for MainPage.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace ChildWindowDemo
{

    public partial class MainPage : UserControl
    {
        private PopupWindow popUp;
        public MainPage()
        {
            InitializeComponent();
            popUp = new PopupWindow();
            popUp.Closed += new EventHandler(popUp_Closed);
            btnShowPopUp.Click += new RoutedEventHandler(btnShowPopUp_Click);

        }

        void btnShowPopUp_Click(object sender, RoutedEventArgs e)
        {
            popUp.Title = "Confirmation";
            popUp.SetMessage("How are you doing today?");
            popUp.Show();
            txtStatus.Text = "";
        }

        void popUp_Closed(object sender, EventArgs e)
        {

            if ((bool)popUp.DialogResult)
            {
                txtStatus.Text = "You have clicked OK Button";
            }
            else
            {
                txtStatus.Text = "You have clicked Cancel Button";
            }
        }
    }
}

  

Full Source code for PopupWindow.xaml

  <controls:ChildWindow x:Class="ChildWindowDemo.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="300" Height="150"
           Title="PopupWindow">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock x:Name="Message" Text="" TextWrapping="Wrap"/>
        <Button x:Name="CancelButton" Content="Cancel" 
                Click="CancelButton_Click" Width="75" 
                Height="23" 
                HorizontalAlignment="Right" 
                Margin="0,12,0,0" Grid.Row="1" />
        <Button x:Name="OKButton" Content="OK" 
                Click="OKButton_Click" Width="75" 
                Height="23" 
                HorizontalAlignment="Right" 
                Margin="0,12,79,0" Grid.Row="1" />
    </Grid>
</controls:ChildWindow>

Full Source code for PopupWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace ChildWindowDemo
{
    public partial class PopupWindow : ChildWindow
    {
        public PopupWindow()
        {
            InitializeComponent();
        }


        public void SetMessage(string theMessage)
        {
            Message.Text = theMessage;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

This is how this works

Get Microsoft Silverlight

Reference Articles