I am trying to make a basic window that uses a WPF TabControl to allow users to add new tabs. I want the end product to look somewhat like the way tabs work in a web browser where the last tab is just a "+" that when clicked it will add a new tab.
I am trying to write the XAML code to set this up and I found that I can specify multiple DataTemplates within "TabControl.Resources" and based on the "DataType" the correct DataTemplate will be used to display the correct view for each tab... but when dealing with the tab headers I can only specify a single DataTemplate for "TabControl.ItemTemplate"
This is what I have so far:
<TabControl ItemsSource="{Binding Tabs}">
<TabControl.Resources>
<!-- If the tab is of type "TabViewModel" I want this content -->
<DataTemplate DataType="x:Type vm:TabViewModel">
<!-- TabView is defined as a separate user control -->
<v:TabView/>
</DataTemplate>
<!-- If the tab is of type "NewTabViewModel" I want this content -->
<DataTemplate DataType="x:Type vm:NewTabViewModel">
<!-- NewTabView is defined as a separate user control -->
<v:NewTabView/>
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemTemplate>
<!-- if the tab is of type "TabViewModel" I want this header -->
<DataTemplate DataType="x:Type vm:TabViewModel">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<!-- If the tab is of type "NewTabViewModel" I want this header -->
<!-- ERROR: Adding a second "DataTemplate" here results in an error -->
<DataTemplate DataType="x:Type vm:NewTabViewModel">
<TextBlock Text="+"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
Googling around I found some articles on setting up a TemplateSelector and writing a bunch of background C# code, but that seems drastically overkill for something this simple. I just want it to display the tab name if it is a regular TabViewModel object and a "+" if it is a NewTabViewModel object.