Social Icons

Pages

Tuesday, November 15, 2011

Passing value from Child window to Parent window

Child Window is a great new feature in silverlight which enables you to use benefit of a fully better popup window. In this article I will explain the simple way how to pass the information child window to parent window

I am going to create an example in which user enters his/her first name and last name in a Child Window and once submitted entered first name and last name are displayed in the main page.

     1. We will start creating new Silverlight application in Visual Studio 2010 
     Click "New   project" in the Menu bar,Select " Silverlight Application" and name the  
     application as "SilverlightChildWindow".

     2. Add a new Child Window 
    In visual Studio right click on your Silverlight project, then, Add ->New Item ->  
    Silverlight Child Window. The Child Window will have an OK and a Cancel button by 
    default  when it is created. Add two textbox controls to the page for users to enter 
    their information.

     3. Create an EventHandler in Child Window
       public event EventHandler SubmitClicked;
    4. Create public properties to save the values
    Here I create two public properties of type string to save the value of both textBox  
    controls.
private string firstName;
  public string FirstName
   {
    get { return firstName; }
    set { firstName = value; }
   }
private string lastName;
  public string LastName
   {
    get { return lastName; }
    set { lastName = value; }
   }
     5. Pass the event once ok button is clicked in Child Window
     There should already be ok button in the child window when button is pressed; it is called  
     ‘OKButton_Click’. Inside that we need to fire the event which we created earlier, so that 
     our event gets fired when the code reaches this method.
    if (SubmitClicked != null)
            {
                FirstName = txt_FirstName.Text;
                LastName = txt_LastName.Text;
                SubmitClicked(this, new EventArgs());
            }
    this.DialogResult = true;
    6. Create a new instance of the Child Window in the MainPage.xaml page
private ChildWindow1 childwindow;
    7. Call the Child Window
    To do that, use the instance, which already created just call the method for Child Window.
private void button1_Click(object sender, RoutedEventArgs e)
        {
            childwindow.Show();
        }
     8. In MainPage.xaml catch up the submit event in Child Window
     Now we have an event which will get fired up from the Child Window when we click the 
     OK  button.
childwindow.SubmitClicked += new EventHandler(childWindow_SubmitClicked);
    9. Access the properties of the Child Window
private void childWindow_SubmitClicked(object sender, EventArgs e)
      {
            txt_FirstName.Text = childwindow.FirstName;
            txt_LastName.Text = childwindow.LastName;
        }

  • Main Page
  • Child Window

    • Again Main Page

      No comments:

      Post a Comment