简体   繁体   中英

Open new (second) window, do something in it and immediately change something in first window WPF C#

I've got two WPF windows and I want to do this: when user clicks on a button, which is located in the first window, a second window will open and in this window the user will click for example on button "Sword". Then in first window's textblock, text will show "Sword" immediately.

I know I can transfer text or string with text file or XML file or class. But I don't know how to do it immediately. Should I use timer or is there another way?

Thanks for reply.

The simpiliest way is to pass first window as a parameter to the constructor of the second window and change properties of first one on the button click event handler.

public class SecondWindow : Window
{
    private readonly FirstWindow _owner;
    public SecondWindow(FirstWindow owner)
    {
        _owner = owner;
    }

    private void OnSwordButtonClick(object sender, EventArgs e)
    {
        _owner.TextBlockValue = "New value";
    }
}

You can use more commplex and universal solution with EventAggregator or other event-based patterns . Event aggregator helps to implement subscriber/publisher pattern in low-coupled app.

First create new event with property that you want to send between components (or without any properties if there is no data in this event).

Next subscribe first window to your event and change your data ( string binded to the Label or other model property) in event handler with parameter received from handler parameter.

Finally raise (or publish in other terms) your event from your second window on button click event handler.

As you can see both of implementations is a part of MVVM libraries and this pattern can be useful for your app.

EDIT

Your code examples shows a problem with NullReferenceException . It happens because you are calling Window1 constructor in the wrong manner. I write full example below but try to repeat again with more details. Change your code to the next one:

public class Window1 : Window
{
    private readonly MainWindow _owner;

    // The only constructor of Window1 class
    public Window1(MainWindow owner)
    {
        InitializeComponent();
        _owner = owner;
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        _owner.TbW1.Text = "Sword1";
    }
}

public class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        // create new instance of Window1 and pass current window as a constructor parameter
        var win1 = new Window1(this);
        win1.Show();
    }
}

Also, there are many recommendations for you.

  1. It's better to use sensible class/method/variable names for example OptionsWindow instead of Window1 OnChooseSwordButtonClick() instead of button_Click. Read more about Naming Guidelines on MSDN .
  2. Read more about C# Coding Conventions on MSDN .
  3. Read more about NullReferenceException .

The term you are looking for is Interprocess Communications (IPC) .

Here is an excerpt from Microsoft :

The Windows operating system provides mechanisms for facilitating communications and data sharing between applications. Collectively, the activities enabled by these mechanisms are called interprocess communications (IPC). Some forms of IPC facilitate the division of labor among several specialized processes. Other forms of IPC facilitate the division of labor among computers on a network.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM