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

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

 Subscribe to Articles

Passing Data from Child Window to Parent Window in Silverlight

Posted By:Diptimaya Patra       Posted Date: March 13, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

Passing Data from Child Window to Parent Window in Silverlight. In this article we will see a sample implementation of this requirement to pass the value or data from the ChildWindow to the Parent Window.


There were many requirements where we need to pass the value from ChildWindow to our Parent Window in Silverlight. In this article we will see a sample implementation of this requirement to pass the value from the ChildWindow to the Parent Window.

This is the idea what we are going to do. We will select a date from the calendar from the ChildWindow and pass the selected value to the Parent Window and display it.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as ChildToParent.


Now first things first add a ChildWindow to the Silverlight Project name the ChildWindow as DatePopUpWindow.


Now that we have added everything needed we will design our page in Blend as follows:


I have added one TextBlock to display the Selected Date and one Image; on it's click the ChildWindow will be fired up.


Now we will design our Childwindow as follows:


As you see from the above image; I have added a calendar to the ChildWindow.


Now go back to the VisualStudio IDE and open DatePopUpWindow.xaml.cs

Here we will add a event handler.

public event EventHandler OkClicked;

Add a property that can be accessed from the main window.

private DateTime _SelectedDate; 
public DateTime SelectedDate
   get { return _SelectedDate; }
   set { _SelectedDate = value; }

Now in the OkButton_Click event add the following code:

private void OKButton_Click(object sender, RoutedEventArgs e)
  if (OkClicked!=null)
  _SelectedDate = (DateTime)myCalendar.SelectedDate;
  OkClicked(this, new EventArgs());
  this.DialogResult = true;

Now go back to the MainPage.xaml.cs and create an instance of ChildWindow:

DatePopUpWindow calendar;

In the click event of the Image (Calendar) display the ChildWindow and Initiate the Event.

private void imgCalendar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  calendar = new DatePopUpWindow();
  calendar.OkClicked += new EventHandler(cw_OkClicked);

In the event handler add the following code to access the data from ChildWindow.

void cw_OkClicked(object sender, EventArgs e)
   txtDate.Text = "You have selected : " + calendar.SelectedDate.ToShortDateString();

That's it. We are done. Now run your application.

When you click on the Calendar Image the ChildWindow will be displayed.


Select any date and press ok.


Hope you like this simple article.
 Subscribe to Articles


Further Readings:


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