Search code examples
wpftabcontroltabitem

WPF Creating types of Tabitems in tabcontrol


i need to create types of Tabitems to my tabcontrol. each tab will have different content and functionalities (Xaml and code-behind). For Example, i want to create: * Customer details tab - with fields of customer detials. * Configuration tab - fields for configuring the application. * Statistics tab - table and graphs with statistics.

Sometimes two or three tabs of each tabitem type will be open. i don't want to copy paste the TabItem.Content again and again for same customer tab or other. i want to make a type of tab.

what is the best way to create such tabitem types ?


Solution

  • Usually I store my TabItemViewModels in a ParentViewModel, and use a DataTemplate to define how each ViewModel should be displayed.

    <Window.Resources>
        <DataTemplate DataType="{x:Type local:CustomerDetailsViewModel}">
            <local:CustomerDetailsView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:ConfigurationViewModel}">
            <local:ConfigurationView />
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:StatisticsViewModel}">
            <local:StatisticsView />
        </DataTemplate>
    </Window.Resources>
    
    <TabControl ItemsSource="{Binding TabList}" SelectedItem="{Binding SelectedTab}" />