Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Navigate from One Page to Another in WPF

Tags:

c#

mvvm

wpf

xaml

I have a MainWindow.XAML and CustomersView.XAML.

When I click the Customer Button on MainWindow , I want to navigate to CustomersView.XAML and palong with that need to pass few parameters.

I can use NavigationService but is only available with Pages and not Window.Hyperlink is not an option at this moment.

This might be fairly simple thing but not sure how can I implement this using MVVM and with out any third party control.

like image 418
Simsons Avatar asked Feb 18 '15 04:02

Simsons


2 Answers

private void Navigate_Click(object sender, RoutedEventArgs e)//By Prince Jain 
{
   this.NavigationService.Navigate(new Uri("Page3.xaml", UriKind.Relative));
}
like image 131
Prince Jain Avatar answered Oct 31 '22 15:10

Prince Jain


There are many options to navigate from one window to another in WPF. You can use a frame in your MainWindow and navigate all your pages right inside your Frame.

<Window 
    x:Class="NavigationSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <DockPanel>
        <Frame x:Name="_mainFrame" />
    </DockPanel>
</Window>

From code, you can tell the frame to navigate, like so:

_mainFrame.Navigate(new Page1());

Which just so happens to be a helpful shortcut to:

_mainFrame.NavigationService.Navigate(new Page1());

Or if you using any framework like PRISM, you are allowed to create a Shell where you can define regions and let your pages navigate to that.

Navigation Using the Prism Library 5.0 for WPF

like image 44
Jawahar Avatar answered Oct 31 '22 13:10

Jawahar