0

So I have to transform the content of a label on a second window to the input of a textbox on the main window. I have no problem doing this on the same window, but I have no idea how to get the data across multiple windows. So far I've multiple things but I couldn't get it to work. This is my code at the moment:

        public void Button_Click(object sender, RoutedEventArgs e)
    {
        label1.Content = textBox1.Text;
        label2.Content = textBox2.Text;
    }

So I want to do the same thing like this, only then the textBoxes are on the MainWindow and the Labels are on Window1. Is there an easy way to do this? If not, what would be a better alternative?

  • "to get the data across multiple windows" you need to assign them the same view model (DataContext), and then use bindings in textBoxes and Labels – ASh Oct 23 '18 at 15:11
  • Are you using MVVM? – d.moncada Oct 23 '18 at 15:14
  • 1
    If you are not yet into bindings and MVVM, then there are still events. Not sure if `TextBox` has one (`TextChanged` was in winforms), but you can use [dependency property changed](https://stackoverflow.com/a/32233156/1997232). – Sinatr Oct 23 '18 at 15:19
  • I'm not using MVVM, but I will definitely look into it! Thanks for the suggestion! – Jan Vlasman Oct 23 '18 at 15:28

2 Answers2

0

The simplest way is to include a reference to the Window1 instance on your MainWindow. The you can write ...

public void Button_Click(object sender, RoutedEventArgs e)
{
    Window1.label1.Content = textBox1.Text;
    Window1.label2.Content = textBox2.Text;
}

... although this can soon evolve into a big ball of spaghetti.

A more elegant solution is to create a ViewModel class with the required string properties, set the DataContext of both MainWindow and Window1 to the same instance of this class, and then bind the TextBoxes and Labels to these properties. No button click is required to keep all of the controls in sync.

Peregrine
  • 4,287
  • 3
  • 17
  • 34
0

If you are displaying Window1 from the MainWindow, you could inject it with a reference to the MainWindow when you create an instance of it:

public partial class Window1 : Window
{
    private readonly MainWindow _mainWindow;
    public Window1(MainWindow mainWindow)
    {
        InitializeComponent();
        _mainWindow = mainWindow;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        label1.Content = _mainWindow.textBox1.Text;
        label2.Content = _mainWindow.textBox2.Text;
    }
}

MainWindow:

Window1 win = new Window1(this);
win.Show();

You could also get a reference to the MainWindow from Window1 like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
    if (mainWindow != null)
    {
        label1.Content = mainWindow.textBox1.Text;
        label2.Content = mainWindow.textBox2.Text;
    }
}

But I recommend you to learn the MVVM design pattern. It is the recommended pattern to use when developing XAML based UI applications.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you so much! And I will definitely check out MVVM. (especially since I was stuck on this for way too long) – Jan Vlasman Oct 23 '18 at 16:08