Search code examples
c#wpfmaster-pages

Main Frame Listening to Page Button Click Event


I have a Main Window, which is the frame that displays different pages in my project, I would like when a button on a page is clicked, to change the page in the frame.

The problem is I can't seem to get the Frame to contain the event handler for when the button is clicked, it seems local to the page itself. Is there a way for the Frame to see when the page's button is clicked and change the page accordingly?

Main.Content = new Page.Intro();

However when the button Next is clicked within Page.Intro() I want within the Main Window Class to the display

Main.Content = new Page.Home(userinformation); 

I tried to follow How to click a button of the page to change the source of the frame of Windows? which is similar to what I'm trying to achieve, although my pages have parameters I need to pass so I can't call MainContent.Source = new Uri("Home.xaml", UriKind.Relative);as there is no where to pass userinformation.


Solution

  • as there is no where to pass userinformation

    Navigate method not only takes a Uri, it has an overload that takes an object as parameter. So this works for you

    //button click in Page.Intro
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.NavigationService?.Navigate(new Page.Home(userinformation));
    }