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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With