Saturday, May 12, 2012

Pass Data from Parent window to ChildWindow using Constructor

In Silverlight, there are different ways to pass data from parent window to child window.
In this post i am explaining one of the way to pass data using parameterized constructor of child window.

1.     Create a new Silverlight Application.
2.     Right-click on the project and select Add New Item.
3.  Select Silverlight Child Window template from the Add New Item dialog and give name to childwindow as MyChildWindow

Now i will pass the values of two text boxes from MainPage to MyChildWindow.
I will use one value to set the title of childwindow and other value will be displayed in MyChildWindow inside a textbox. 

My MainPage.xaml is like following
   <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Pass Data to Child Window" Height="22" HorizontalAlignment="Left" Margin="42,120,0,0" Name="btnShowChildWindow" VerticalAlignment="Top" Width="158" Click="btnShowChildWindow_Click" />
        <dataInput:Label Height="25" HorizontalAlignment="Left" Margin="42,43,0,0" Name="label1" VerticalAlignment="Top" Width="118" Content="ChildWindow Title" />
        <dataInput:Label Height="28" HorizontalAlignment="Left" Margin="42,74,0,0" Name="label2" VerticalAlignment="Top" Width="173" Content="Pass Data to the ChildWindow " />
        <TextBox x:Name="txtTitle" Height="23" HorizontalAlignment="Left" Margin="221,43,0,0" VerticalAlignment="Top" Width="120" />
        <TextBox x:Name="txtData" Height="23" HorizontalAlignment="Left" Margin="221,74,0,0" VerticalAlignment="Top" Width="120" />

Here i have a button btnShowChildWindow and on click of it i will pass the values of textboxes to childwindow and will open up the child window with these values.

In MainPage.xaml.cs add code as follows
private void btnShowChildWindow_Click(object sender, RoutedEventArgs e)
            MyChildWindow objChildWindow = new MyChildWindow(txtTitle.Text, txtData.Text);

My MyChildWindow.xaml is like following
<Grid x:Name="LayoutRoot" Margin="2">
            <RowDefinition />
            <RowDefinition Height="Auto" />

        <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" />
        <dataInput:Label Height="18" HorizontalAlignment="Left" Margin="12,86,0,0" Name="label1" VerticalAlignment="Top" Width="174" Content="Data Passed from MainPage is " />
        <TextBlock Height="18" HorizontalAlignment="Left" Margin="227,86,0,0" x:Name="txtData" VerticalAlignment="Top" Width="139" />

In MyChildWindow.xaml.cs add the constructor to receive the values from Parent window and set these values to the title of child window and textblock control inside child window.
public MyChildWindow(string strTitle, string strName)
            this.Title = strTitle;
            this.txtData.Text = strName;

That it...Once you run the application your application should pass data to child window as shown in below image..

Hope this helps...

No comments:

Post a Comment