SIGN UP MEMBER LOGIN:    
ARTICLE

Silverlight Child Window - Passing Data Between Child And Parent

Posted by Mahadesh Mahalingappa Articles | Silverlight with C# August 07, 2011
In this article we are going to see the Child window control in Silverlight. This example is taken from Silverlight 4 unleashed and implemented.
Reader Level:
Download Files:
 


Create a Silverlight Project named SLChildControl.

Add a ChildWindow Control as shown in the figure:

Silverlight application

A childWindow Control gets added as shown below:

add Silverlight Child Window

Add the following code to MyChildWindow.xaml.cs

      public const string InputPropertyName = "Input";
      public string Input
      {
         get
         {
             return (string)GetValue(InputProperty);
         }
         set
         {
             SetValue(InputProperty, value);
         }
      }
     
      public
static readonly DependencyProperty InputProperty
      = DependencyProperty.Register(
      InputPropertyName,typeof(string),typeof(MyChildWindow),new PropertyMetadata("Enter text..."));

Now modify the MainPage.xaml as shown below:

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="136,124,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="120,36,0,0" Name="InputTextBlock" VerticalAlignment="Top" Width="120" />
    </
Grid>

Modify the Code Behind as follows:

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var window = new MyChildWindow();
            window.Closed += new EventHandler(window_Closed);
            window.Show();
        }
        void window_Closed(object sender, EventArgs e)
        {
            var window = sender as MyChildWindow;
            if (window != null && window.DialogResult == true)
            {
                InputTextBlock.Text = window.Input;
            }
        }

Let's give it a run:

run Silverlight application

Hit the Button. The child window should open.

Silverlight Child Window

Hit OK.

Child Window in silverlight

The TextBox in the MainPage gets updated with the text "Enter text ….".

How this works is, well, when you hit the ok or close button on the child, the child close event is called and it assigns the text to the textbox of the MainPage.

Silverlight Child Window
  

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor