如何在WPF中的两个页面之间传递值

在WPF中的页面之间传递值的最佳做法是什么?

谢谢

您的固定参考点是Application对象。 您可以在Properties集合中存储内容:

string myText = (string)Application.Current.Properties["test"]; 

或者,您可以向派生的App类添加任何类型的数据。

可能是通过MVVM架构中的Model。

示例变量name = DeptName

在App.xaml中声明变量即

public string DeptName { get; set; }

在页面1中指定值

 (App.Current as App).DeptName = "test"; 

然后调用第2页中的值

  string selected_dept = (App.Current as App).DeptName; 

与Windows窗体相同:

不要只使用全局变量或从另一个页面访问页面控件。 如果你有两个页面需要共享同一个对象,例如Student ,你的页面中有一个类似SetStudent(Student student) ,或者使用一个属性,这样一个页面就可以使用该方法传递对象Student。 如果需要,您也可以获得Get。