-4

I have two Windows: Window1 and Window2 and I have to pass data between these two. In detail: There are some TextBoxes in Window2, in which the user should enter some code. On clicking the ok-button, the entered data should be added to a list and then displayed in a ListBox. Now my question is how to get the data from Window2 to Window1? Thanks for Your answers.

More generally: what is a good way to share data between different forms/windows of a WPF application.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

4

You have all kinds of options:

  • Push Reference Window1 in Window2 and put the right data into the controls in Window1 when the ok button in Window2 is clicked.
  • Pull Add an EventListener to the button_ok_click from Window2 in Window1 and a reference to Window2 and read the values from Window2 when the ok button is clicked
  • Share Use a ViewModel and share the data of this ViewModel in Window1 and Window2 (this way you can get automatic updates when you use INotifyPropertyChanged in your ViewModel which is pretty standard and straightforward)

Hope this helps a bit...

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • Thank you for the answer. Which one would you suggest? – gillesberger_p Jan 01 '15 at 21:52
  • 1
    @gillesberger_p I would suggest the one you understand :) – L.B Jan 01 '15 at 22:02
  • When you use WPF in a bigger more complex application you should really go the way OF MVVM (Model View ViewModel) which is an excellent framework to set up high-quality applications. You can find loads of basic examples about this in WPF. But, as always you have to decide if it's worth the energy. Don't know your experience level, the kind of application, the importance of it, etc. etc. The first two are a bit more amateurish but will do the job pretty well... – Youp Bernoulli Jan 02 '15 at 10:37