简体   繁体   中英

WPF - How to use ContentControl with TabControl MVVM

I'm learning WPF MVVM and I can't solve this problem so I hope you can help me.

So without TabControl everything is working fine but when I used it like in the code below then I see only string System.Windows.DataTemplate and ContentControl also is string with ViewModel name.

I tried to change ContentControl position but nothing help. How to fix it? Please help.

  <Grid>
    <TabControl>
        <TabItem Header="View">
            <DataTemplate DataType="{x:Type vm:View1Model}">
                <view:View1/>
            </DataTemplate>
        </TabItem>
        <TabItem Header="Add">
            <DataTemplate DataType="{x:Type vm:View2Model}">
                <view:View2/>
            </DataTemplate>
        </TabItem>
    </TabControl>
    <ContentControl Content="{Binding CurrentViewModel}" />
</Grid>

A DataTemplate is used together with the ItemsSource property of an ItemsControl.

A TabControl is an ItemsControl so you could use this (but your syntax is not correct). That would require a collection containing View1Model and View2Model.

When I understand your case correctly, you don't actually need templates:

<TabControl>
    <TabItem Header="View">
            <view:View1/>
    </TabItem>
    <TabItem Header="Add">
            <view:View2/>
    </TabItem>
</TabControl>

you may have to set up a DataContect for the Views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM