[ Log In ]
Skip Navigation Links

Transfer data from a child window to a parent window

The easiest way I have found to pass data from a child window to a parent window is to use an application wide property. This property is an object, and is not the most elegant form to pass data from a child window to a parent, but it's the least amount of programming. The best way to do this is using get and set accessor properties.

Create a main window (mainWindow)
Create a child window (in this case, Password)

In the main window, the child window must be shown, say, within a button click. This window would have a button to do something, in this case, it's to delete a record from the database.

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
Password passwordentry = new Password();
passwordentry.ShowDialog();

if (Application.Current.Properties["PassGate"].ToString() == "mypassword")
{
Code, or call to delete the record;
}
Application.Current.Properties["PassGate"] = "";
}

In the child window (Password), the property for the application is set using a textbox. This is a simple window that has a textbox called PasswordTextBox and a couple of buttons, like Accept and Cancel.

private void AcceptButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["PassGate"] = PasswordTextBox.Text;
this.Close();
}