Pass value to Child Window form Parent Page

2k Views Asked by At

I Need pass the Value to child Window.In child window there is two Text boxes.I need to show the value in child window Text box while while its getting opened.

I tried the following way,My Child window class as follows,

    public partial class ChildWindow:ChildWindow
    {
    public int abc {get;set;}
    public string value{get;set;}
    public ChildWindow()
    {
    InitializeComponent();          
    this.txtbox1.Text =  abc ; 
    this.txtbox2.Text =  value; 
    }

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
    this.DialogResult = true;

    }

    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
    this.DialogResult = false;
    }

    }

My Parent Window as follows,

    private void EditButton_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    {
    ChildWindow child= new ChildWindow();
    child.abc = 1;
    child.alue = "Hello"
    child.show();
    }

How can I show the child window controls with values (which is getting from Parent) while its getting opened?

2

There are 2 best solutions below

1
Joel Legaspi Enriquez On BEST ANSWER

You can change the following:

public int abc {get;set;}
public string value{get;set;}

To:

public int abc
{
    get
    {
        int result = 0;
        int.TryParse(this.txtbox1.Text, out result);
        return result;
    }
    set
    {
        this.txtbox1.Text = value;
    }

}


public string value
{
    get
    {
        return this.txtbox2.Text;
    }
    set
    {
        this.txtbox2.Text = value;
    }

}

The problem in your code is, the properties are being assigned during initialization of the control. Not when the properties has been changed.

0
Mairaj Ahmad On

You can create an overload of constructor.

public ChildWindow(string abc,string value)
{
InitializeComponent();          
this.txtbox1.Text =  abc ; 
this.txtbox2.Text =  value; 
}

Than create object of childwindow like this

ChildWindow child= new ChildWindow("abc","somevalue");