James
James

Reputation: 31778

How do you call a function in the parent page from a child page within a frame?

I have a page (called MainPage) that has a frame in it containing an instance of a child page (called EditorPage). How can the child page instance call a public function in the parent page?

Upvotes: 2

Views: 1141

Answers (1)

paparazzo
paparazzo

Reputation: 45106

Not pretty but you can pass a reference in the ctor

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Page1 page1 = new Page1(this);
        Frame1.Navigate(page1);
    }
    public void MainMethod() {}
}


public partial class Page1 : Page
{
    public Page1()
    {
        InitializeComponent();
    }
    public Page1(MainWindow mw)
    {
        mw.MainMethod();
        InitializeComponent();
    }
}

Consider just adding Class to the project to hold shared methods

Upvotes: 3

Related Questions