In WPF I have one window opening a second window that allows some data selections to occur on. Once the selections have been made I then want to submit/close the second/sub window and have the data pushed back to the opening/parent window.
I can push data into the sub window from the parent via public properties, but have not worked out how to get it back the other way. Can anyone help please?
The code I am using to open the window and set some properties is has follows:
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 that has a button that when clicked brings up a list of email contacts. It brings through any previously selected email addresses from the first window and auto selects them on the sub window/email list. If further selections are made on the email list, when the op clicks the done/submit/close button it pushes the new selection list back into the relevant TextBox on the first/parent window.
Thanks
I would suggest you define a event in appContacts that you can then subscribe to from your calling class. The Event would then have the result from your dialog in it's EventArgs.
This saves you the check for "was closed through OK rather than Cancel" in your calling class.
In Code this would look like this: First you create the 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;
}
}
You then define an event on your SubWindow and create a method to invoke that event if there are event subscribers (if there are any you construct the 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 your Parent window you now can simply hook up to the eventhandler when opening the window:
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 that in pure C# classes, so in a real-world WPF app Subwindow would be a child of Frame or something, but I hope it illustrates the point I'm trying to make.
You now only have to call the OnDialogFinished
method inside your subwindow when you want to pass the data back and close the window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With