WPF C# Frame Navigation from page navigated

24,209

Solution 1

I guess the scenario here is that he has a Window with a frame and the navigation method is also in this window.

What you can do is to pass your window to those dynamically created pages.

//Main Window
public class MainWindow
{
   public void NavigatePage(Page page)
   {
      YourFrameInstance.Navigate(page)
   }
 }

//Your Page
public class Page
{
    private readonly Window _mainWindow;

    public Page(Window mainWindow)
    {
       _mainWindow = mainWindow;
    }

    void Button_Click(object sender, RoutedEventArgs e)
    {
        var page = new Page(_mainWindow);
        _mainWindow.NavigatePage(page);
    }
 }

Solution 2

If I understand correctly, you want to navigate from one page to another using a event?

If that is the case, take a look at the following links below - these will give you a better understanding of navigating through a WPF application providing examples and sample apps.

How to Build, Manage and Navigate the User Interface of a WPF Application

Simple Navigation

This is how I achieve it. In the code behind of your application, do something like this to help navigate to the next page for your click event;

private void btnClick_HomeClick(object sender, RoutedEventArgs e)
{
     FrameContent.Navigate(new ExampleView()); //FrameContent is the name given to the frame within the xaml.
}

Hope this helps!

Share:
24,209
loris91
Author by

loris91

Updated on July 09, 2022

Comments

  • loris91
    loris91 almost 2 years

    i've a windows and a frame in this windows. the frame navigate in a page and into this page i've a button. The page was created dinamically so, i want to add a event onclick of button into the page, that permit me to navigate a new page into frame of window.

    i'm writing on google about custom event that can permit me to raise an event in a page that is related to event in a window.

    Can you help me please??

  • loris91
    loris91 about 11 years
    hi gregory, thanks for your answer, i should navigate from one page to others page but with event into one class . in a class i assigned a event onclick to a button, and in this event i must raise an event in mi mainwindow that contain a frame! hope you can hel me!
  • greg
    greg about 11 years
    Sorry, I don't really understand what you're trying to say. I recommend you go through those two links I suggested or try and implement the code snippet I created for you. If I understand you correctly, you have multiple pages you want to navigate to. If thats the case, you could use a Menu or a Ribbon and use events to navigate through each page. Hope this helps.
  • loris91
    loris91 about 11 years
    i've resolved with this stackoverflow.com/questions/7901007/…
  • Wobbles
    Wobbles over 7 years
    Want to point out, that depending on what is on the page and how often you call for navigation, this can cause a memory leak as you are creating new instances of that class for each navigation without disposing of it. I like to use new Uri("ExampleView.xaml", UriKind.Relative) more because it seems to clean up after itself.