Reputation: 119
I have a main window and would like to pass a value to a Pop Up window (which will use that value to set up some other values dependant on that value). The pop up window has multiple textboxes for user inputs. When a button called SaveButton is pressed I would like the user inputs (all of them) to be sent back to the main window. How can I accomplish this?
I am very new to C# and I am aware that there are similar questions. However, I am having difficulties with adapting the answers to my specific situation. Thank you for keeping that in mind when answering!
Thank you so much!
Edit: About the code The code that I have: For initiating the PopUp window (which is a Window WPF called PopUp)
PopUp popUp = new PopUp();
popUp.ShowDialog
The PopUp.xaml.cs is empty except for the standard stuff and an eventhandler for the click event of the button.
Upvotes: 2
Views: 2857
Reputation: 63
In general terms the most simplified solution is this one:
From MainWindow to Child window (Window1);
((Window1)Application.Current.Windows[1]).testlabel.Content = "In Sync";
From child window to MainWindow;
((MainWindow)Application.Current.MainWindow).label2.Content = "whatever";
Upvotes: 0
Reputation: 2487
Look at this :
Your main window :
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//Defining public or internal parameters
public int MainWindowProp1 { get; set; }
public string MainWindowProp2 { get; set; }
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
//When you call your child window, pass this (mainwindow) as parameter.
childWindow cw = new WpfApplication1.childWindow(this);
cw.Param2 = "test";
cw.Param1 = 12;
cw.Closed += Cw_Closed;
cw.ShowDialog();
}
private void Cw_Closed(object sender, EventArgs e)
{
//On closed event, you can cast the sender as your child window.
var child = (sender as childWindow);
var param1 = child.Param1;
var param3 = child.Param2;
}
public void TestMethod()
{
//do anything you want
}
}
}
And your Child Window :
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for childWindow.xaml
/// </summary>
public partial class childWindow : Window
{
//declare Mainwindow as a parameter in your child window
public MainWindow mainWindow;
public int Param1 { get; set; }
public string Param2 { get; set; }
//Add a parameter in your child window contructor.
public childWindow(MainWindow _mainWindow)
{
InitializeComponent();
//Assign your global parameter mainWindow to the _mainWindow parameter.
this.mainWindow = _mainWindow;
this.Loaded += ChildWindow_Loaded;
}
private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
{
//You can get or set your main window properties.
this.mainWindow.MainWindowProp1 = 5;
this.mainWindow.MainWindowProp2 = "test";
//You can call methods ant events etc.. of your main window too (depending on acces modifiers).
this.mainWindow.TestMethod();
}
}
}
Upvotes: 3