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 :)

No comments:

Post a Comment

Followers