How to navigate between windows in WPF?

34,805

Solution 1

NavigationService is for browser navigation within WPF. What you are trying to do is change to a different window TrainingFrm.

To go to a different window, you should do this:

private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
    var newForm = new TrainingFrm(); //create your new form.
    newForm.Show(); //show the new form.
    this.Close(); //only if you want to close the current form.
}

If, on the other hand, you want your WPF application to behave like a browser, then you would need to create Pages instead of Forms, and then use a Frame in your application to do the navigation. See this example.

Solution 2

If you want to navigate from Window to Window:

private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
    Window1 window1 = new Window1();
    // window1.Show(); // Win10 tablet in tablet mode, use this, when sub Window is closed, the main window will be covered by the Start menu.
    window.ShowDialog();
    }

If you want to navigate from Window to Page:

private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
   NavigationWindow window = new NavigationWindow();
   window.Source = new Uri("Page1.xaml", UriKind.Relative);
   window.Show();
}

Solution 3

In order to use NavigationService you should use the Page and not the Window class

Share:
34,805
Brian Var
Author by

Brian Var

I'm a creative web developer with 5+ years’ experience in front-end development and a keen interest in developing responsive dashboards. I've constructed contact center, administration and reporting web applications using the latest JavaScript frameworks. I have good experience in performing requirements scoping.

Updated on July 19, 2022

Comments

  • Brian Var
    Brian Var almost 2 years

    I have tried to set up a click event for a button that opens another window,but the error I'm getting at NavigationService is that the project doesn't contain a definition for it.

    This is how I'm trying to call the page at present:

    private void conditioningBtn_Click(object sender, RoutedEventArgs e)
    {
        this.NavigationService.Navigate(new Uri("TrainingFrm.xaml", UriKind.RelativeOrAbsolute));
    }
    

    Can someone point me in the right direction with this or show alternatives to this method for window navigation?

  • Brian Var
    Brian Var over 10 years
    That worked anyways thanks :) I just got a book on WPF so hopefully Ill gain a better understanding of it.