user2980509
user2980509

Reputation: 152

How to pass data between WPF forms

I need help passing data from one WPF form to another. I have a main window with two other windows that will prompt the user for information. I want to end up with all the information in the first form so that I can store the data later on. The second form must return the Reservation and Room information when you click the OK button on the second form. The third form must return the Person information when you click OK.

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();

Here it is supposed to set the room, reservation and person instance that I created equil to their corresponding instances in the CreateResRoom class.

I think the problem lies here, because it keeps continuing before it opens the CreateResRoom form.

PersonRoom = createReservationRoom.myRoom;
        PersonReservation = createReservationRoom.myReservation;
    }
}

That was my first class, the second and third will follow.

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();

//I think the same problem lies here that is in the MainWindow.

        myPerson = createReservation.myPerson;
        this.Close();
    }
}

And the last class follows:

 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();
    }
}

Upvotes: 0

Views: 12814

Answers (4)

Newman
Newman

Reputation: 1

I found another answer that Zarathos posted Jan 16 '13 at 21:43 for a different question

Use a public static class and access it from anywhere.

public static class Globals
{

   public static String s_Name = "Mike";  //Modifiable in Code

   public const int32 VALUE = 10;  // unmodifiable

}

Then you can use it anywhere, provided you are working on the same namespace

string name = Globals.s_Name;

Upvotes: 0

Just make a overload constructor which takes parameters of the window in which you want to retrieve. Example:

Suppose we want a user to login from our MainWindow( i.e Login Window ) and we want to pass an int ID / string Email to our second form to retrieve data of logging user. Than We have to first overload our second wpf form constructor. You can either make default constructor to do this or make an overload constructor for this work.

SecondForm:

   public secondForm()
   {
    //Your Default Constructor Logic
   }
   public secondForm(string email_ )
   {
    //Your Overload Constructor Logic
   }

Now in MainWindow from where we are logging and passing our EMail

MainWindow:

   public void btnLogin()
    {
        //On Success

        SecondWindow sw = new SecondWindow(txtBoxEMail.Content);
        sw.Show();
    }

Upvotes: 3

CyborgDE
CyborgDE

Reputation: 1

Use the "normal way", here is a short overview.

First create a Data Context:

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 ) );
        }
    }

}

In the MainWindows you can assign and use the 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();
   }
}

You can assign the DataContext in the constructor, but I think the better way is to define the DataContext in the MainWindow, in the other windows you can use the DesignContext:

<Window.DataContext>
   <local:DC_Reservation />
</Window.DataContext>

So you can use the same DataContext over all forms ...

With DataBindings you can bind the input to the field:

<TextBox Text="{Binding FirstName, Path=myPerson, Mode=TwoWay}" />

Upvotes: 0

bingles
bingles

Reputation: 12183

A pattern you can use for this sort of thing is to have each form be responsible for creating the instance on ok click and then provide the object via a property 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();
    }
}

Then you would access it from a parent form like this (notice the use of ShowDialog() http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v=vs.110).aspx for easy checking of whether ok was clicked or not).

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

Upvotes: 0

Related Questions