Ok, let's say I have two windows. In the first one I have a method
public void Test()
{
Label.Content += " works";
}
And in the second one I call this method:
MainWindow mw = new MainWindow();
mw.Test();
But nothing happens. What am I doing wrong? Thanks.
You can assign the Owner to the window that was created in your MainWindow.
window.Owner = this; //This is added to the code that use to create your Window
Then you should be able to access it something like this.
((MainWindow)this.Owner).Test();
MainWindow
public partial class MainWindow : Window
{
Window1 window = new Window1();
public MainWindow()
{
InitializeComponent();
window.Show();
}
public void Test()
{
label1.Content += " works";
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
window.Owner = this;
}
}
Second Window
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
((MainWindow)this.Owner).Test();
}
}
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