Siddhant
Siddhant

Reputation: 581

Navigating to different views in same window in WPF

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

Answers (3)

Sir Rufo
Sir Rufo

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

Tony Vitabile
Tony Vitabile

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

Jophy job
Jophy job

Reputation: 1964

you can use TabControl or Page control for this.

MVVM-TabControl

Closeable-TabItems

Upvotes: 0

Related Questions