Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I have a button on a Page, the Page is loaded inside a Frame (WPF).

Button -> Page - > Frame

When I click on the button I want to open another page inside my Frame (main frame)

how can I do this?? kindly help :)
Posted
Updated 10-Nov-11 2:33am
v3

1 solution

You can use the VisualTreeHelper class to find the Frame that contains the Page and, set the Source property of the Frame to the new Page.


You can implement the Click event-handler of your Button, as the following:


C#
private void btnChangePage_Click(object sender, RoutedEventArgs e)
{
    // Find the frame.
    Frame pageFrame = null;
    DependencyObject currParent = VisualTreeHelper.GetParent(this);
    while (currParent != null && pageFrame == null)
    {
        pageFrame = currParent as Frame;
        currParent = VisualTreeHelper.GetParent(currParent);
    }

    // Change the page of the frame.
    if (pageFrame != null)
    {
        pageFrame.Source = new Uri("Page2.xaml", UriKind.Relative);
    }
}

 
Share this answer
 
Comments
goog4peace 10-Nov-11 9:53am    
@Shmuel Zang: thank you my friend :) you made it dear.
Adil Alsuhaim 24-Mar-15 2:45am    
Thanks!! This solution actually worked. However instead of

pageFrame.Source = new Uri("Page2.xaml", UriKind.Relative);

I used:
pageFrame.Navigate(typeof(Page2), null);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900