简体   繁体   中英

How to use get, set in Windows Presentation Foundation (WPF)

I have two windows form, and I can transfer data between them by using (get, set), the code is in below:

Form1.cs

public partial class Form1 : Form
{
    public TextBox tb1
    {
        get { return textBoxinForm1; }
        set { textBoxinForm1 = value; }
    }

    private void buttonSendtoForm2_Click(object sender, EventArgs e)
    {
        Form2 form2 = (Form2)Application.OpenForms["Form2"];
        form2.tb2.Text = textBoxinForm1.Text;
    }
}

Form2.cs

public partial class Form2 : Form
{
    public TextBox tb2
    {
        get { return textBoxinForm2; }
        set { textBoxinForm2 = value; }
    }

    private void buttonSendtoForm1_Click(object sender, EventArgs e)
    {
        Form1 form1 = (Form1)Application.OpenForms["Form1"];
        form1.tb1.Text = textBoxinForm2.Text;
    }
}

When I try to do the same but with windows (WPF), I have error below "textBoxinForm2", "textBoxinForm1"and "value". So, how to fix that..

The corresponding WPF code to get a reference to a Window1 class would be this:

private void buttonSendtoForm1_Click(object sender, EventArgs e)
{
    Window1 form1 = Application.Current.Windows.OfType<Window1>().FirstOrDefault();
    form1.tb1.Text = textBoxinForm2.Text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM