Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass values between two pages in WPF

Tags:

c#

wpf

c#-4.0

what is the best practice to pass values between pages in WPF?

thanks

like image 632
Arian Avatar asked Oct 26 '11 11:10

Arian


4 Answers

Your fixed point of reference is the Application object. You can store stuff in the Properties collection:

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

Or you can add any kind of data to your derived App class.

like image 60
Henk Holterman Avatar answered Oct 28 '22 04:10

Henk Holterman


Example variable name = DeptName

Declare the variable in App.xaml i.e.

public string DeptName { get; set; }

assign the value in your page-1

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

then call the value in your page-2

 string selected_dept = (App.Current as App).DeptName;
like image 40
Raj Avatar answered Oct 28 '22 04:10

Raj


Probably via the Model in an MVVM architecture.

like image 41
Joe Avatar answered Oct 28 '22 05:10

Joe


same as Windows Forms:

do not just use global variables or access page's controls from another page. if you have two pages which need to share the same object, e.g. Student, have a method like SetStudent(Student student) in your page or use a property so one page can pass the object Student using that method. You can also have the Get of course, if needed.

like image 1
Davide Piras Avatar answered Oct 28 '22 06:10

Davide Piras