C# 从窗口外部更改var的值

C# 从窗口外部更改var的值,c#,wpf,C#,Wpf,在WPF上,我有打开form1的窗口form2 在表格2上我有 public partial class form2 : Window { public int temp1; public form2() { InitializeComponent(); temp1 =123 ; this.tempTextBox.Text = temp1.ToString();

在WPF上,我有打开
form1
的窗口
form2

表格2上
我有

 public partial class form2 : Window
    {

        public int temp1;

        public form2()
        {
            InitializeComponent();

            temp1 =123 ;
            this.tempTextBox.Text = temp1.ToString();

        }

    }
form1
上,我想打开
form2
但编辑
temp1的值
我想做的是:

    Window newWind = new form2();
    (newWind as form2).temp1=555;
    newWind.Show();
但是当
表单2
打开时,我看到在
文本框中
=123

我想看看那里
555

请问我怎么做


谢谢

将其更改为属性,修改setter中的文本框文本

private int _temp1;
public int temp1{
get { return _temp1; }
set { 
    _temp1= value; 
    this.tempTextBox.Text = value;
    }
}

但是
temp1
的值是多少。。。您必须将
tentextbox
的值重置为
temp1
才能查看更改您正在设置temp1值,但我认为您从未设置过文本框文本。在初始化表单“this.testextbox.Text=temp1.ToString()”时,将其设置为123,但随后设置了变量,该变量对更改文本框没有任何作用。我想如果你把一个断点放进去并检查temp1,它会保持555。您只需要更新文本框。您正在将
testextbox.Text
设置为该值,而不是绑定它。要正确设置绑定,您需要a)实现INotifyPropertyChanged以便属性更改通知工作,b)使
temp1
成为具有get/set访问器的属性,以及c)将
tentextbox.Text
绑定到
temp1
属性。网上有很多这样的例子。或者,只需让属性的set访问器更新文本框值,正如Aaron所说。这是正确的答案。解释原因:在构造函数中设置temp1字段中的文本,然后在以后更改temp1字段时无法重新设置文本。