How to send data from an open window back to its opener in WPF

In WPF, I have one window opening a second window that allows some data selections. After you have made a selection, I want to send / close the second / sub window and return the data to the open / parent window.

I can push data into the helper window from the parent via public properties, but haven't worked out how to get it back in another way. Can anyone please help?

The code I am using to open the window and set some properties is the following:

Dim wdwContacts As New appContacts()
wdwContacts.selClientID = selClientID
wdwContacts.selEmailToCCType = selEmailToCCType
wdwContacts.pullToAddresses = txEmailTo.Text
wdwContacts.pullCCAddresses = txEmailCC.Text
wdwContacts.Owner = Me
wdwContacts.WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner
wdwContacts.Show()

      

The basis is that I have the first window as an email composition page, which has a button that, when clicked, brings up the email contact list. It takes over all previously selected email addresses from the first window and automatically selects them in the subframe / email list. If additional selections are made in the email list, when the op clicks the "done / submit / close" button, it returns the new selection list in the corresponding text box in the first / parent window.

thanks

+2


a source to share


2 answers


You can set properties in the class appContacts

, then handle the Close event window and read these properties in the opening window.



+1


a source


I would suggest you define an event in appContacts, which you can then subscribe to in your calling class. The event will then receive the result from your dialog in EventArgs.

This will allow you to check that "was closed with OK and not Cancel" in your calling class.

In Code, it looks like this: first, you create an EventArgs class that will encapsulate the return data from your window:

public class WindowEventArgs : EventArgs
{
    private readonly string username;
    private readonly string password;
    public string Username { get; private set; }
    public string Password { get; private set; }

    public WindowEventArgs(string username, string password)
    {
        this.username = username;
        this.password = password;
    }
}

      

Then you define an event in your SubWindow and create a method to call that event if there are event subscribers (if there are any, you create an EventArgs class and pass it to the event):



public class SubWindow
{
    public event EventHandler<WindowEventArgs> DialogFinished;

    public void OnDialogFinished()
    {
        if (DialogFinished != null)
            DialogFinished(this, new WindowEventArgs("some username", "some password"));
    }
}

      

In the Parent window, you can simply connect to an event handler when the window is opened:

public void OpenSubWindow()
{
    var subWindow = new SubWindow();
    subWindow.DialogFinished += new EventHandler<WindowEventArgs>(subWindow_DialogFinished);
}

void subWindow_DialogFinished(object sender, WindowEventArgs e)
{
    //Do Something with the results you get from e.Username & e.Password
}

      

I obviously did this in pure C # classes, so in a real WPF application, Subwindow will be a child of Frame or whatever, but I hope this illustrates what I am trying to do. Now you only need to call a method OnDialogFinished

within your subset when you want to pass data back and close the window.

+5


a source







All Articles