Pass value to Child Window form Parent Page

可紊 提交于 2019-12-11 07:59:57

问题


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?


回答1:


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.




回答2:


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");


来源:https://stackoverflow.com/questions/31645283/pass-value-to-child-window-form-parent-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!