Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parent frame from page level?

I have a window with a frame and few pages. When the window is loaded the frame is navigating to welcome page and when I click a button inside welcome page I want parent frame to navigate to another page. To do that I need to access parent frame from page level but I can't figure out how to do this. I tried code below but it's returning null:

private void myButton_click(object sender, RoutedEventArgs e)
{
  SecondPage secPage = new SecondPage();
  ((this.Parent) as Frame).Navigate(secPage);
}

I checked what this.parent is returning and it is null. How can I get parent frame so I can navigate from one page to another?

like image 242
MajkeloDev Avatar asked Mar 17 '23 21:03

MajkeloDev


1 Answers

A Page also has a property called NavigationService. You can use this property for convenience to navigate between pages:

private void myButton_click(object sender, RoutedEventArgs e) {
  SecondPage secPage = new SecondPage();
  NavigationService.Navigate(secPage);
}

To access the parent Frame, you have to use VisualTreeHelper to walk up, and find the Frame. You can search more about it. Anyway using the NavigationService property is better.

like image 97
King King Avatar answered Mar 28 '23 04:03

King King