-2

I have 2 WPF windows. Both the windows have textboxes and combo boxes. Upon entering data into Window1, the user presses a "Next" button and Window2 is loaded. Window2 has a "Back" button which will reload Window1 incase the user wants to change some values. Since it is the same session, I want the last entered values in window1 to appear when the "Back" button in Window2 is pressed. How should I go about doing it in C# or XAML?

Julyflowers
  • 133
  • 2
  • 13

2 Answers2

0

you have to take one property in window 1 like

 public string ChangeValue { get; set; }

when you press back buttion at that time you can set the property value using instance of window 1

instanceofWindow1.ChangeValue="Value";
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
0

Each window should have its own ViewModel. These contains the values you type in the windows. If you click next (and destroy the first window) the you should implement some kind of save method (ICommand that is invoked when clicking on Next) that saves the current state of the ViewModel to a model class (or a database or a text file ...). When you click back you have to reload the model, connect it with the ViewModel and show the Window1.

It seams you are not familiar with WPF, maybe the links from another answer might help you to get started: https://stackoverflow.com/a/2034333/1015350

Furthermore you should get into the topics:

  • MVVM
  • ICommands
  • Databinding

Rule of thumb: Your code behind file should contain nothing. This is all done by databinding.

Community
  • 1
  • 1
0xBADF00D
  • 980
  • 1
  • 12
  • 25
  • How to create a ViewModel? What is a model class? – Julyflowers May 20 '14 at 08:17
  • Here is a [small overview](https://en.wikipedia.org/wiki/Model_View_ViewModel) over the MVVM pattern. After that you should continue reading [this][http://msdn.microsoft.com/en-us/magazine/dd419663.aspx]. Sorry but explaining the whole concept behind WPF is far to much. In short: the model represents your data (e.g. a user in a database) and the ViewModel prepares the data from the Model for the View. So the View can use Databinding to automatically update the View, when data changes occure. – 0xBADF00D May 20 '14 at 08:27
  • Maybe you should also consider reading a book on this topic. I have the predecessor of "Pro WPF 4.5 in C#: Windows Presentation Foundation in .NET 4.5" and it's pretty good. – 0xBADF00D May 20 '14 at 08:29
  • 1
    MVVM may be overkill for this particular scenario. It seems that the OP is learning WPF coming from a Windows Forms background. Something simpler like saving to text files when changing windows, or creating a global variable/resource in App.xaml would be easier and suffice for this (not necessarily best practice when your app grows larger) – jingtao May 20 '14 at 09:53
  • Also "your code behind file should contain nothing" is a myth - it is necessary to know when to break out from the pattern for sake of practicality. – jingtao May 20 '14 at 09:54
  • Unfortunately I saw lots of people (including myself) with WinForms background that used their old techniques with the new technology. That might be good to get started, but it becomes difficult in the future. My advice: try to start at the basics. – 0xBADF00D May 20 '14 at 12:01