0

I am pretty new in WPF C#. I am held up with the following Issue:

Lets say I have a MainWindow.xaml, and have the main program logic in it.

I have a second window, called Second.xaml

I am calling Second.xaml in MainWindow.xaml.cs, currently I am doing:

MainWindow.xaml.cs:

var wind = new Second();
wind.Show();

This successfully opens the second window, where I have a few buttons. My motive is to trigger events in MainWindow using Second.xaml.cs

(i.e.) in Second.xaml.cs:

....
..
MainWindow mainwindowID = new MainWindow();
....
..
.

private void nextButton_Click(object sender, RoutedEventArgs e)
{
    mainwindowID.textBox.Content = "Displaying In Mainwindow";
}

When I click the Next button in Second.xaml, I want the text Box inside Mainwindow to be updated.

Though the program in running, nothing changes inside MainWindow. Is it possible to control it like that?

I have the MainWindow displayed using a projector, and the second window on the monitor. So I trigger events inside the second window and want them to be displayed in the MainWindow.

Is there any other solution for this kind?

Update: If the textbox is inside SecondPage.xaml and displayed inside MainWindow.xaml using a Frame, how do I call it from Second.xaml?

2 Answers2

2

In the first window (MainWindow) you can invoke the second window in this way:

var wind = new Second();
wind.FirstWindow = this;
wind.Show();

while the second window can look like this:

public MainWindow FirstWindow { get; set; }

private void nextButton_Click(object sender, RoutedEventArgs e)
{
    FirstWindow.textBox.Content = "Displaying In Mainwindow";   
}
romanoza
  • 4,775
  • 3
  • 27
  • 44
  • Thank you this works! If I have the textbox inside anotherPage.xaml and displayed using a Frame inside MainWindow.xaml, What to do? – Viswa Sekar Feb 05 '16 at 12:05
  • @ViswaSekar I think you should ask another question because it's a different problem. You can also browse these answers: http://stackoverflow.com/questions/22080482/how-can-i-access-a-pages-content-inside-a-frame or http://stackoverflow.com/questions/29802911/communicate-between-pages-in-wpf – romanoza Feb 08 '16 at 12:32
0

I would suggest using Delegates. Have a look at the link here;

Delegates

This way you can create a method in the first Window like so;

private void WaitForResponse(object sender, RoutedEventArgs e)
{
    var second = new SecondWindow();
    second.ReturnTextBoxText += LoadTextBoxText;
    SecondWindow.ShowDialog();
}

Then in your second Window;

internal Action<string, int> ReturnTextBoxText;

private void nextButton_Click(object sender, RoutedEventArgs e)
{
    ReturnTextBoxText("Displaying In Mainwindow");   
}

Finally load that response in your first Window;

private void LoadSelectedCompany(string text, int passedCompanyID)
{
    contactCompanyTextBox.Text = companyName;
}
CBreeze
  • 2,925
  • 4
  • 38
  • 93