You can display anything in a NavigationWindow
, not just Pages
. A simple way to make it work is to define in the NavigationWindow
's resources a DataTemplate
for each ViewModel you want to display. Bind the Content
property of the NavigationWindow
to a property of your main ViewModel, and you're done : changing that property will update the NavigationWindow
content, and the appropriate DataTemplate
will be picked automatically
UPDATE
I just looked at the code of a project of mine where I used a NavigationWindow
. Actually I was mistaken, it doesn't work by binding the Content
(or maybe it works, but that's not what I did). Instead I created a INavigationService
interface, implemented by my App
class, which handles the navigation by calling the NavigationWindow.Navigate
method. That way, the navigation history is maintained by the NavigationWindow
.
Here's an extract from my project
MainWindow.xaml :
<NavigationWindow x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MyApp.ViewModel"
xmlns:view="clr-namespace:MyApp.View"
Title="{Binding Content.DisplayName, RelativeSource={RelativeSource Self}, FallbackValue=The Title}"
Height="600" Width="800">
<NavigationWindow.Resources>
<DataTemplate DataType="{x:Type vm:HomeViewModel}">
<view:HomeView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:CustomerViewModel}">
<view:CustomerView />
</DataTemplate>
</NavigationWindow.Resources>
</NavigationWindow>
App.xaml.cs :
...
private void Application_Startup(object sender, StartupEventArgs e)
{
LoadConfig();
MyApp.MainWindow window = new MainWindow();
INavigationService navigationService = this;
HomeViewModel viewModel = new HomeViewModel(navigationService);
this.MainWindow = window;
window.Navigate(viewModel);
window.Show();
}
When I need to navigate to another view, I just call the Navigate
method with the ViewModel as a parameter, and WPF automatically picks the appropriate DataTemplate
from the resources.