我有一个TabControl,通过数据绑定获取选项卡和内容。我想让所有选项卡的标签不是普通的标签,而是自定义形状(例如,椭圆形)。因此,我有一个控件模板,希望将其应用于所有选项卡。作为WPF的新手,我不知道如何在使用数据绑定时将控件模板应用于TabControl的TabItem标头?
<Window.Resources>
<ControlTemplate x:Key="TabItemControlTemplate1" TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="True">
<Border CornerRadius="12" x:Name="Bd" BorderBrush="Red" BorderThickness="1" Padding="{TemplateBinding Padding}">
<ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
</Border>
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<TabControl
ItemsSource="{Binding CarCollection}">
<TabControl.ItemTemplate>
<!-- this is the header template-->
<DataTemplate>
<TextBlock
Text="{Binding DisplayName}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<!-- this is the body of the TabItem template-->
<DataTemplate>
<ContentControl
Content="{Binding Value}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
在不使用数据绑定的情况下,我知道如何应用它。问题是当使用数据绑定时,我可以通过以下代码实现。
<TabControl HorizontalAlignment="Left" Height="224" Margin="72,66,0,0" VerticalAlignment="Top" Width="412">
<TabItem Header="TabItem" Template="{DynamicResource TabItemControlTemplate1}" Height="19.96" VerticalAlignment="Top">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
这有什么需要帮忙的吗?
{DynamicResource TabItemControlTemplate1}
,同时又在使用TabControl.ContentTemplate
来处理选项卡项。它们不会冲突吗? - AntonK