0

Hello i am working on project have two windows the first is the main windows and it contain a public method and the other is secondary windows and contain a button .. what i want is when i press the button on the secondary windows the button will call the method on the main windows here what i try

the code of the main windows :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    public void the_public_method()
    {
        //code 
    }

and the code of the secondary windows :

public partial class windows2 : Window
{
    public windows2()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        // dont know what to put here 
    }
}

Please help

elie mardelly
  • 79
  • 2
  • 10
  • See the marked duplicate for a number of useful approaches. The original question was expressed in the context of Winforms, but the basic techniques described there apply equally well to WPF, and indeed in most cases to any set of related classes, whether UI objects or not. In WPF you also have the option of using your view models and commands to interact, but there's not enough detail or context in your question to provide any suggestions along those lines. – Peter Duniho Oct 01 '16 at 18:08

1 Answers1

2

Easily do this:

(Application.Current.MainWindow as MainWindow).the_public_method()

And done!

Mohammad Mirmostafa
  • 1,720
  • 2
  • 16
  • 32