Saturday, May 19, 2012

Pass Data from Parent window to Child window using Public Property


In previous post i had explained how to pass data from parent xaml to childwindow xaml using parameterised constructor of child window.Here i am explaining the same(pass data from parent xaml to childwindow xaml) with the help  of public property.

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
In MyChildWindow.xaml.cs add a public property which we can access from MainPage.xaml.cs
         public string ChildWindowProperty
        {
            get;
            set;
        }

4. In MainPage.xaml.cs set above created property with data to be passed.

Set public property availble in childwindow.
         private void btnShowChildWindow_Click(object sender, RoutedEventArgs e)
        {
            MyChildWindow objChildWindow = new MyChildWindow();
            objChildWindow.ChildWindowProperty = "This is data set from ParentWindow...";
            objChildWindow.Show();
        }


5.  Now Display/Use the data in child window MyChildWindow.xaml.cs. I have displayed the data available in ChildWindowProperty (which was set from MainPage.xaml) in the textbox named txtData.
         private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.txtData.Text = ChildWindowProperty;
        }

Hope this helps...Happy coding :)

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" />
    </Grid>

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);
            objChildWindow.Show();
        }


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

        <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" />
    </Grid>

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)
        {
            InitializeComponent();
            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...


Followers