.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Silverlight Child Window - Passing data between Child and Parent ( Dependency Property )

Posted By:Mahadesh Mahalingappa       Posted Date: August 07, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

In this article we are going to see the Child window control in Silverlight and pass data between child and Parent window
 


Silverlight Child Window - Passing data between Child and Parent ( Dependency Property ):

In this article we are going to see the Child window control in Silverlight .This example is taken from silverlight 4 unleashed and implemented.


Created a Silverlight Project named as SLChildControl .

Add a ChildWindow Control as shown in the fig :



A childWindow Control gets added as shown below :





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 :


        

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;
    }
}

lets give it a run :





Hit the Button . Child window should open .




Hit OK .




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 .




 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend