Viswa Sekar
Viswa Sekar

Reputation: 1

Two windows display and control WPF C#

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?

Upvotes: 0

Views: 167

Answers (2)

CBreeze
CBreeze

Reputation: 2983

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;
}

Upvotes: 0

romanoza
romanoza

Reputation: 4862

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";   
}

Upvotes: 2

Related Questions