如何在 Windows Presentation Foundation (WPF) 中使用 get、set [英] How to use get, set in Windows Presentation Foundation (WPF)
本文介绍了如何在 Windows Presentation Foundation (WPF) 中使用 get、set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个窗体,我可以用(get, set)在它们之间传输数据,代码如下:
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;
}
}
当我尝试使用 Windows (WPF) 执行相同操作时,在textBoxinForm2"、textBoxinForm1"和value"下方出现错误.那么,如何解决这个问题..
When I try to do the same but with windows (WPF), I have error below "textBoxinForm2", "textBoxinForm1"and "value". So, how to fix that..
推荐答案
获取对 Window1
类的引用的相应 WPF 代码如下:
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;
}
这篇关于如何在 Windows Presentation Foundation (WPF) 中使用 get、set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文