Reputation: 1802
How can I make DataTemplate in WPF for TabItems and in each TabItem customize its content?
I need this:
<TabControl>
<TabControl.ContentTemplate>
<DataTemplate>
<Label Content="Name" Name="label1" />
<TextBox Name="name" />
...
</DataTemplate>
</TabControl.ContentTemplate>
<TabItem Header="Add" Name="tabItem1">
<Grid Height="213">
<Button Content="Add" Name="button1" />
</Grid>
</TabItem>
<TabItem Header="Edit" Name="tabItem2">
<Grid>
<Button Content="Edit" Name="button2" />
</Grid>
</TabItem>
</TabControl>
but the buttons are not displaying (only content of DataTemplate).
Upvotes: 4
Views: 11251
Reputation: 2597
You could also try using TabControl ContentTemplateSelector.
Here, you could define two or more different templates (user controls) for a tab item. And you could also decide this on run time using DataTemplateSelector
Here is a blog which contain a demo of the same : mvvm-using-contenttemplateselector-in-tab-control-view/
The final TabControl would look something like this:
<UserControl x:Class="TabControlItemTemplateDemo.View.MyTabControl"
xmlns:ViewModel="clr-namespace:TabControlItemTemplateDemo"
xmlns:View1="clr-namespace:TabControlItemTemplateDemo.View" mc:Ignorable="d" >
<UserControl.Resources>
<DataTemplate x:Key="MyTabHeaderTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Header}" Width="80" Height="25" FontWeight="Bold"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="CountryContentTemplate">
<View1:CountryView DataContext="{Binding CurrentMyTabContentViewModel}"/>
</DataTemplate>
<DataTemplate x:Key="ContinentsContentTemplate">
<View1:ContinentsView DataContext="{Binding CurrentMyTabContentViewModel}"/>
</DataTemplate>
<ViewModel:MyViewSelector x:Key="selector"
CountryTemplate="{StaticResource CountryContentTemplate}"
ContintentsTemplate="{StaticResource ContinentsContentTemplate}" />
</UserControl.Resources>
<DockPanel>
<TabControl ItemsSource="{Binding Tabs}" TabStripPlacement="Left"
BorderThickness="0" Background="White"
ItemTemplate="{StaticResource MyTabHeaderTemplate}"
ContentTemplateSelector="{StaticResource selector}">
</TabControl>
</DockPanel>
Upvotes: 1
Reputation: 17385
Put the reoccurring template as a resource in the TabControl, and then reference it from the specific Tab's ContentTemplate
using a ContentPresenter
:
<TabControl>
<TabControl.Resources>
<DataTemplate x:Key="TabTemplate">
<Label Content="Name" Name="label1" />
</DataTemplate>
</TabControl.Resources>
<TabItem Header="Add" Name="tabItem1">
<TabItem.ContentTemplate>
<DataTemplate>
<Grid Height="213">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Add" Name="button1" />
<ContentPresenter Grid.Row="1" ContentTemplate="{StaticResource TabTemplate}"/>
</Grid>
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
<TabItem Header="Edit" Name="tabItem2">
<TabItem.ContentTemplate>
<DataTemplate>
<Grid Height="213">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Edit" Name="button2" />
<ContentPresenter Grid.Row="1" ContentTemplate="{StaticResource TabTemplate}"/>
</Grid>
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
Everything besides the ContentPresenter
can be different in every tab...
Upvotes: 1