1

I have been trying to move data from one WPF window to another while both are open without making a new instance. This feels like it should be really easy but after hours of researching I cant get anything to work.

Here is what I have tried so far. This works, however, it creates another mainform.

Dim mainform As New MainWindow
mainform.TextBox3.Text = TextBox1.Text
mainform.Show()

if I try this without the word new it gives me an error.

I am totally puzzled by this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    you don't "move data between windows" in WPF because [UI is Not Data](http://stackoverflow.com/a/14382137/643085). – Federico Berasategui Mar 08 '14 at 19:30
  • If you want to share some data or pass it to another form there are a million and one ways to do it, many of which will be clunky and some of which will be convoluted and just plain wrong. I'd start by trying to understand object oriented principles such as types and instanced/static variables and then attempt to work from there. Understanding these principles will help you write code that makes sense without the guesswork – Charleh Mar 08 '14 at 19:35
  • 1
    It is not called a form in WPF. The time to get and save a reference to the Window is when it is created. How/when does the other Window get created? – paparazzo Mar 08 '14 at 20:42

2 Answers2

1

I suggest you take a look at the MVVM concept, which will give you a great foundation for understanding how to setup your WPF application. It appears that you lack the basic understanding of WPF components necessary to achieve proper results. While there are many ways of developing WPF application, MVVM will provide that necessary base. Moreover, I suggest looking at MSDN and other websites that would provide beginner tutorials. By reading your question, I feel that you're trying to do something without really know what you're doing. I don't suggest that it's a bad thing in any way - I believe that the best way to learn is by doing, but you need to acquire some guidance in a manner of tutorials or perhaps a book.

B.K.
  • 9,982
  • 10
  • 73
  • 105
1

Use MVVM pattern. Your ViewModel will contain all data that you need to be copied to the other window. Your Window class will have a constructor that accepts a ViewModel. Now just write a copy constructor for your ViewModel, create a new Window with that copy, and you're done.

Ryan Burbidge
  • 192
  • 3
  • 13
  • TextBox2.Text = Application.Current.Windows.OfType(Of MainWindow)().First().TextBox2.Text thats what i went with in the end – user3062970 Mar 16 '14 at 14:41