Reputation: 581
I have a WPF application that follows MVVM. I am able to design separate windows, but how can I navigate between different views in the same main window instead of launching every view in a different window. For ex - Consider, I have 2 views. Window1.xaml and Window2.xaml. Consider the third view CommonWindow.xaml. Now, within this CommonWindow.xaml I want to provide a toggle button. When I click this button it should load the Window1 and Window2 alternately within the CommonWindow.xaml. How can I achieve this? Is there any particular control in WPF that supports this kind of behavior?
Upvotes: 0
Views: 1454
Reputation: 19106
You can bind the ViewModel to a ContentControl
and add some DataTemplates for each ViewModel.
<Window ...
xmlns:view="clr-namespace:SO31437431.View"
xmlns:viewmodel="clr-namespace:SO31437431.ViewModel"
...>
<Grid>
<ContentControl Content="{Binding CurrentItem}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewmodel:BarViewModel}">
<view:BarView/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodel:FooViewModel}">
<view:FooView/>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
</Window>
Upvotes: 0
Reputation: 8594
Rather than using a button to toggle between view windows inside of a container window, why not make the view windows controls and put them in a TabControl
in the common window? The XAML would look like this:
<TabControl>
<TabItem>
<local:MyView1>
</TabItem>
<TabItem>
<local:MyView2>
</TabItem>
</TabControl>
Upvotes: 1