简体   繁体   中英

set the content of a frame from different page - WPF C#

I have a frame called mainFrame which is inside MainWindows . I have a page called page1 , when the app starts, I dynamically set the content of the mainFrame to page1 .

Now, suppose I have another page called page2 . I have a button inside page1 , I want When I click on that button, mainFrame 's content changes to page2 , But I can't access mainFrame from page1 .

How can I do this?

To achieve this you need to get current instance of your MainWindows.

Inside your click event handler for button in Page1 write :

MainWindows objMainWindows = (MainWindows)Window.GetWindow(this);
objMainWindows.mainFrame.Navigate(Page2);

The 'NavigationService' instance is available from the page.

// Inside page1
private void ClickEventHandler()
{
    NavigationService.Navigate(page2);
}

Or if you want to return control flow to the mainwindow before changing the page, look into PageFunction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM