MajkeloDev
MajkeloDev

Reputation: 1661

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?

Upvotes: 5

Views: 8024

Answers (1)

King King
King King

Reputation: 63327

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.

Upvotes: 9

Related Questions