locked
How to data from parent window to child window RRS feed

  • Question

  • Hi,

    I am new to silverlight. I  need to know how to display the values from an array variable(which is in a parent window) in a child window?
    I need to display the values, one by one in child window and on the right side I need to display controls( which is similar to any registration form).
    Also a save option should be available in the child window which should save the content to a text file.

    Thanks in advance!
    Tuesday, October 20, 2009 12:14 PM

Answers

  • Hi Arul

    The parent window's controls and variables can be accessed in the child window using "this.Parent" in the child window. Type cast this.Parent as the Parent window class. Your array variable in the parent window will then be available in the child window.

    Sample Code:

    Parent Window: ParentWindow1
    Child Window: ChildWindow1

    Code in parent:


    public

     

    string Data { get; set; }

    void

     

    ParentWindow1_Loaded(object sender, RoutedEventArgs e)
    {
      Data = "Some information in parent";
      ChildWindow
    chWin = new ChildWindow1();
      chWin.Closed +=
    new EventHandler(chWin_Closed);
      chWin.Show();
    }

     

     

     


    Child window code:

    void

     

    ChildWindow1_Loaded(object sender, RoutedEventArgs e)
    {
      ParentWindow1 parent = this.Parent as ParentWindow1;
      TxtData.Text = parent.Data; // TxtData is a textblock control to show the parent data.
    }

     


    The same logic applies to using the data in the child window within the parent window. THe closed event's sender object is the child window class. Type cast it and access the data from the parent.

    If this has answered your question please mark it as answered. Thanks in advance.

    Ashwatthama

    • Proposed as answer by Ashwatthama Wednesday, October 21, 2009 7:19 AM
    • Marked as answer by Arul SAS Wednesday, October 21, 2009 7:36 AM
    • Unmarked as answer by Arul SAS Wednesday, October 21, 2009 10:14 AM
    • Marked as answer by Arul SAS Wednesday, October 21, 2009 11:07 AM
    Wednesday, October 21, 2009 7:17 AM

All replies

  • Hi Arul

    The parent window's controls and variables can be accessed in the child window using "this.Parent" in the child window. Type cast this.Parent as the Parent window class. Your array variable in the parent window will then be available in the child window.

    Sample Code:

    Parent Window: ParentWindow1
    Child Window: ChildWindow1

    Code in parent:


    public

     

    string Data { get; set; }

    void

     

    ParentWindow1_Loaded(object sender, RoutedEventArgs e)
    {
      Data = "Some information in parent";
      ChildWindow
    chWin = new ChildWindow1();
      chWin.Closed +=
    new EventHandler(chWin_Closed);
      chWin.Show();
    }

     

     

     


    Child window code:

    void

     

    ChildWindow1_Loaded(object sender, RoutedEventArgs e)
    {
      ParentWindow1 parent = this.Parent as ParentWindow1;
      TxtData.Text = parent.Data; // TxtData is a textblock control to show the parent data.
    }

     


    The same logic applies to using the data in the child window within the parent window. THe closed event's sender object is the child window class. Type cast it and access the data from the parent.

    If this has answered your question please mark it as answered. Thanks in advance.

    Ashwatthama

    • Proposed as answer by Ashwatthama Wednesday, October 21, 2009 7:19 AM
    • Marked as answer by Arul SAS Wednesday, October 21, 2009 7:36 AM
    • Unmarked as answer by Arul SAS Wednesday, October 21, 2009 10:14 AM
    • Marked as answer by Arul SAS Wednesday, October 21, 2009 11:07 AM
    Wednesday, October 21, 2009 7:17 AM
  • Alternatively,

    Have a constructor in the child window which will accept the array in the parent as a parameter. You can then use this data in the child window and contruct your controls.

    Ashwatthama
    • Proposed as answer by Ashwatthama Wednesday, October 21, 2009 7:27 AM
    Wednesday, October 21, 2009 7:27 AM
  • I dont understand what do you mean by constructor, since Iam very new to Silverlight.
    If possible can you explain by sample code?

    This is My code in parent window:

    public partial class MainPage : UserControl
        {
            private ChildWindow1 popupWindow;
            private string[] returnval;
            public string[] arrayval   
            {
                get { return returnval; }
                set { returnval = value; }
            }
            public MainPage()
            {
                InitializeComponent();
                //object ob = this.arrayval;
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            }
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
               popupWindow = new ChildWindow1();          
            }

            private void button_Click(object sender, RoutedEventArgs e)
            {          
                TextBlock txtblk = new TextBlock();
                Type txtblktype = typeof(TextBlock);
                txtblk.Height = 40;
                txtblk.Width = 100;
                txtblk.Text = "Text Block";
                LayoutRoot.Children.Add(txtblk);           
                PropertyInfo[] txtblkinfo = txtblktype.GetProperties();
                Char[] dlm = { ' ' };
                string txtnew  = null;
                for (int i = 0; i < txtblkinfo.Length; i++)
                {
                    if (txtnew == null)
                        txtnew = txtblkinfo[i].ToString().Substring(txtblkinfo[i].ToString().IndexOf(' ')) + "|";
                    else
                       txtnew = txtnew  + txtblkinfo[i].ToString().Substring(txtblkinfo[i].ToString().IndexOf(' ')) + "|";
                }
                returnval = txtnew.Split('|');
                txtblk.MouseLeftButtonDown +=new MouseButtonEventHandler(txtblk_MouseLeftButtonDown);              
            }

            private void txtblk_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                popupWindow.Title = "Properties";
                popupWindow.Show();
            }
        }

    this arrayval is the variable having list of values.

    Child window:

    public partial class ChildWindow1 : ChildWindow
        {
            public ChildWindow1()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(ChildWindow1_Loaded);
            }

            void ChildWindow1_Loaded(object sender, RoutedEventArgs e)
            {
                blk1.Text = arrayval;
            }

    I need the list those values in child window(in a text block).
    but when I call arrayval like this I stil get the same error. error-the name arrayval doesnot exist in the current context.

    I tried the samples what you gave, like:

      void ChildWindow1_Loaded(object sender, RoutedEventArgs e)
            {
                MainPage parent = this.Parent as MainPage;
                blk1.Text = arrayval;
            }

    hope this is what you meant. but stil the same error I get.

    Thanks for your response.

    Wednesday, October 21, 2009 7:44 AM
  • Is there anything specifically I need to mention since I use an array variable?

    Like,

    blk1.Text = arrayval as Array;

    or anything like this?

    Wednesday, October 21, 2009 7:51 AM
  • I dont understand what do you mean by constructor, since Iam very new to Silverlight.
    If possible can you explain by sample code?

    This is My code in parent window:

    public partial class MainPage : UserControl
        {
            private ChildWindow1 popupWindow;
            private string[] returnval;
            public string[] arrayval   
            {
                get { return returnval; }
                set { returnval = value; }
            }
            public MainPage()
            {
                InitializeComponent();
                //object ob = this.arrayval;
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            }
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
               popupWindow = new ChildWindow1();          
            }

            private void button_Click(object sender, RoutedEventArgs e)
            {          
                TextBlock txtblk = new TextBlock();
                Type txtblktype = typeof(TextBlock);
                txtblk.Height = 40;
                txtblk.Width = 100;
                txtblk.Text = "Text Block";
                LayoutRoot.Children.Add(txtblk);           
                PropertyInfo[] txtblkinfo = txtblktype.GetProperties();
                Char[] dlm = { ' ' };
                string txtnew  = null;
                for (int i = 0; i < txtblkinfo.Length; i++)
                {
                    if (txtnew == null)
                        txtnew = txtblkinfo[i].ToString().Substring(txtblkinfo[i].ToString().IndexOf(' ')) + "|";
                    else
                       txtnew = txtnew  + txtblkinfo[i].ToString().Substring(txtblkinfo[i].ToString().IndexOf(' ')) + "|";
                }
                returnval = txtnew.Split('|');
                txtblk.MouseLeftButtonDown +=new MouseButtonEventHandler(txtblk_MouseLeftButtonDown);              
            }

            private void txtblk_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                popupWindow.Title = "Properties";
                popupWindow.Show();
            }
        }

    this arrayval is the variable having list of values.

    Child window:

    public partial class ChildWindow1 : ChildWindow
        {
            public ChildWindow1()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(ChildWindow1_Loaded);
            }

            void ChildWindow1_Loaded(object sender, RoutedEventArgs e)
            {
                blk1.Text = arrayval;
            }

    I need the list those values in child window(in a text block).
    but when I call arrayval like this I stil get the same error. error-the name arrayval doesnot exist in the current context.

    I tried the samples what you gave, like:

      void ChildWindow1_Loaded(object sender, RoutedEventArgs e)
            {
                MainPage parent = this.Parent as MainPage;
                blk1.Text = arrayval;
            }

    hope this is what you meant. but stil the same error I get.

    Thanks for your response.

    Wednesday, October 21, 2009 7:52 AM
  • I still have problem in the code..

     UserControl parent = this.Parent as UserControl;
     blk1.Text = parent.arrayval;

    when I tried with this, I got the below error:

    'System.Windows.Controls.UserControl' does not contain a definition for 'arrayval' and no extension method 'arrayval' accepting a first argument of type 'System.Windows.Controls.UserControl' could be found (are you missing a using directive or an assembly reference?)

    Thanks
    Wednesday, October 21, 2009 10:20 AM
  • Hi Arul

    public partial class ChildWindow1 : ChildWindow
        {
            public ChildWindow1()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(ChildWindow1_Loaded);
            }

            void ChildWindow1_Loaded(object sender, RoutedEventArgs e)
            {
                blk1.Text = arrayval;
            }


    Add the below code to your child window.

    Hi Arul

    public partial class ChildWindow1 : ChildWindow
        {
            public string _parameter {get;set;}
            public ChildWindow1(string parameter)
            {
                InitializeComponent();
                _parameter = parameter
                this.Loaded += new RoutedEventHandler(ChildWindow1_Loaded);
            }

     void ChildWindow1_Loaded(object sender, RoutedEventArgs e)
            {
                blk1.Text = _parameter;
            }


    in parent window:

    PopupWindow = new ChildWindow1(arrayval);


    This will work.

    Ashwatthama

    Thursday, October 22, 2009 2:29 PM