Wednesday, 4 March 2015

How to pass data between WPF forms

Introduction

This article will help you to find out issues related to questions like this.
 
If you have any issue similar to above questions than this article may help you in some way.

Using the code

Just make a overload constructor which takes parameters of the window in which you want to retrieve value.
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(); 

  }