我需要帮助将数据从一个WPF表单传递到另一个表单。我有一个主窗口,其他两个窗口会提示用户提供信息。我希望最终获得第一个表单中的所有信息,以便稍后可以存储数据。单击第二个表单上的“确定”按钮时,第二个表单必须返回“预订”和“房间”信息。单击“确定”时,第三个表单必须返回“人员”信息。
public partial class MainWindow : Window
{
private string message;
public MainWindow()
{
InitializeComponent();
}
protected void Exit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
protected void Create_Reservation_Click(object sender, RoutedEventArgs e)
{
Reservation PersonReservation = new Reservation();//Create a reservation instance
Room PersonRoom = new Room(); //Create an instance of a room
Person myPerson = new Person();//Create an instance of a person
CreateResRoom createReservationRoom = new CreateResRoom();//Create a instance of the CreateReservation WPF Form
createReservationRoom.Show();
这里应该将我创建的房间,预订和人物实例设置为与CreateResRoom类中的相应实例相等。
我认为问题出在这里,因为它在打开CreateResRoom表单之前会继续存在。
PersonRoom = createReservationRoom.myRoom;
PersonReservation = createReservationRoom.myReservation;
}
}
这是我的第一堂课,第二堂课和第三堂课将会跟进。
public partial class CreateResRoom : Window
{
Person myPerson;
public CreateResRoom()
{
InitializeComponent();
myReservation = new Reservation();
myRoom = new Room();
myPerson = new Person();
}
public Room myRoom
{
get;
set;
}
public Reservation myReservation
{
get;
set;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
myRoom.RoomBeds = txtHeadCount.Text;
myRoom.RoomNumber = 1;
myRoom.RoomPrice = 20;
myRoom.RoomType = cboRoomType.Text;
myReservation.ResEndDate = dpEnd.ToString();
myReservation.ResStartDate = dpStart.ToString();
CreateRes createReservation = new CreateRes();
createReservation.Show();
//我认为MainWindow中存在同样的问题。
myPerson = createReservation.myPerson;
this.Close();
}
}
最后一节是:
public partial class CreateRes : Window
{
public Person myPerson
{
get;
set;
}
public CreateRes()
{
InitializeComponent();
myPerson = new Person();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
myPerson.FirstName = txtFName.Text;
myPerson.LastName = txtLName.Text;
myPerson.IdNumber = Convert.ToInt32(txtIdNumber.Text);
myPerson.PhoneNumber = Convert.ToInt32(txtPhoneNumber.Text);
myPerson.AddressCity = txtAddressCity.Text;
myPerson.AddressStreet = txtAddressStreet.Text;
myPerson.AddressProvince = txtAddressProvince.Text;
myPerson.AddressPostalCode = txtAddressPostalCode.Text;
this.Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
答案 0 :(得分:3)
只需创建一个重载构造函数,该构造函数接受您要检索的窗口的参数。 例如:
假设我们希望用户从我们的MainWindow(即登录窗口)登录,并且我们希望将int ID /字符串Email传递给我们的第二个表单以检索日志记录用户的数据。 比我们必须首先重载我们的第二个wpf表单构造函数。您可以使默认构造函数执行此操作,也可以为此工作创建重载构造函数。
<强> SecondForm:强>
public secondForm()
{
//Your Default Constructor Logic
}
public secondForm(string email_ )
{
//Your Overload Constructor Logic
}
现在在MainWindow,我们正在记录并传递我们的电子邮件
<强>主窗口:强>
public void btnLogin()
{
//On Success
SecondWindow sw = new SecondWindow(txtBoxEMail.Content);
sw.Show();
}
答案 1 :(得分:0)
您可以用于此类事情的模式是让每个表单负责在ok点击上创建实例,然后通过属性get提供对象。
public partial class SomeForm: Window
{
public SomeClass MyProperty { get; private set; }
private void btnOk_Click(object sender, RoutedEventArgs e)
{
this.MyProperty = new SomeClass();
//additional setter logic here
this.Close();
}
}
然后你可以从像这样的父表单访问它(注意使用ShowDialog()http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v=vs.110).aspx来轻松检查是否点击了ok。)
protected void Create_Reservation_Click(object sender, RoutedEventArgs e)
{
SomeClass myObj;
SomeOtherClass myOtherObj;
SomeForm myForm = new SomeForm();
if(myForm.Show().Value)
{
myObj = myForm.MyProperty;
}
SomeOtherForm myOtherForm = new SomeOtherForm();
if(myOtherForm.ShowDialog().Value)
{
myOtherObj = myOtherForm.MyOtherProp;
}
//save myObj & myOtherObj or whatever you need to do with them
答案 2 :(得分:0)
使用“常规方式”,这是一个简短的概述。
首先创建一个数据上下文:
public class DC_Reservation() : INotifyPropertyChanged {
protected Reservation _PersonReservation ;
public Reservation PersonReservation {
get { return _PersonReservation ; }
set {
_PersonReservation = value;
NotifyPropertyChanged("PersonReservation ");
}
}
protected Room _PersonRoom ;
public Room PersonRoom {
get { return _PersonRoom ; }
set {
_PersonRoom = value;
NotifyPropertyChanged("PersonRoom");
}
}
protected Person _myPerson ;
public Person myPerson {
get { return _myPerson ; }
set {
_myPerson = value;
NotifyPropertyChanged("myPerson ");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged( string PropertyName ) {
if ( PropertyChanged != null ) {
PropertyChanged( this, new PropertyChangedEventArgs( PropertyName ) );
}
}
}
在MainWindows中,您可以分配和使用dataContext:
public partial class MainWindow : Window {
DC_Reservation dataContext {
get { return DataContext as DC_Reservation; }
}
private string message;
public MainWindow() {
InitializeComponent();
DataContext = new DC_Reservation();
}
protected void Create_Reservation_Click(object sender, RoutedEventArgs e) {
dataContext.PersonReservation = new Reservation();//Create a reservation instance
dataContext.PersonRoom = new Room(); //Create an instance of a room
dataContext.myPerson = new Person();//Create an instance of a person
CreateResRoom createReservationRoom = new CreateResRoom();//Create a instance of the CreateReservation WPF Form
// I'm not sure whether the next line is required.
createReservationRoom.DataContext = DataContext;
createReservationRoom.Show();
}
}
您可以在构造函数中分配DataContext,但我认为更好的方法是在MainWindow中定义DataContext,在其他窗口中您可以使用DesignContext:
<Window.DataContext>
<local:DC_Reservation />
</Window.DataContext>
因此,您可以在所有表单上使用相同的DataContext ...
使用DataBindings,您可以将输入绑定到字段:
<TextBox Text="{Binding FirstName, Path=myPerson, Mode=TwoWay}" />
答案 3 :(得分:0)
我找到了另一个答案,Zarathos在1月16日和13日在21:43发布了这个问题 对于另一个问题
使用公共静态类并从任何地方访问它。
public static class Globals
{
public static String s_Name = "Mike"; //Modifiable in Code
public const int32 VALUE = 10; // unmodifiable
}
然后,您可以在任何地方使用它,前提是您正在使用相同的命名空间
string name = Globals.s_Name;