WPF

Parameter from SubWindow to MainWindow via Event

Last I was asked if it was possible to create communication between 2 windows and give a parameter with the event instead of firing an event and check manually the value of the property in the SubWindow.

And of course there is a solution to that problem.

The solution for this is by making use of a Delegate.

The problem with the standard event is that you can’t give extra parameters with the event. Standard you only have the Sender and the EventArgs. If you want to pass an extra parameter you need to create a delegate.

In this example I make use of a simple WPF application with a MainWindow and SubWindow. The MainWindow will create an instance of the SubWindow and when I change the value of the TextBox in the SubWindow the value of the label in the MainWindow will change with it.

First of all we need to create a new WPF project.
You can do this via ‘File –> New –> Project’ and select ‘WPF Application’ from the list of templates.
After you created the project you can add a new WPF Window to your application called SubWindow.

Than we can start coding.

The first thing you need to do is create an event on the SubWindow screen.

The interface for the SubWindow contains a textbox (for the input of the user) and a button (to launch the event).

image

In the code behind of your SubWindow you need to create delegate and an event property.
In the Click event of the button you can launch the event with the inputValue fo the textbox as a parameter.

!Remark: Best practice is to check if the event is null because if no screen has subscribed to the event you will get an error.

So by implementing this your code should look like this:

public partial class SubWindow : Window
{
    public delegate void CustomEvent(string inputValue);
    public event CustomEvent SubmitEvent;

    public SubWindow()
    {
        InitializeComponent();
    }

    private void btnSubmit_Click(object sender, RoutedEventArgs e)
    {
        if (SubmitEvent != null)
            SubmitEvent(txtValue.Text);
    }
}

After implementing the SubWindow we can now implement our Button_Click event of the MainWindow.

Here in the Button event we will create an instance of the SubWindow and subscribe to the event.

By doing this the code of your MainWindow should look like this:

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

    private void btnNewScreen_Click(object sender, RoutedEventArgs e)
    {
        SubWindow subwindow = new SubWindow();
        subwindow.SubmitEvent += new SubWindow.CustomEvent(ShowSubmittedInputValue);
        subwindow.Show();
    }

    public void ShowSubmittedInputValue(string submittedValue)
    {
        lblInputValue.Content = submittedValue;
    }
}

And now when you run the application, create an instance of the SubWindow and submit a value, the value will be shown in the MainWindow trough the use of an event with parameters.

image

One thought on “Parameter from SubWindow to MainWindow via Event

Leave a Reply

Your email address will not be published. Required fields are marked *