有人可以帮助我。 我有一个TabItem标题的已编写样式。 但是因为我的TabItem Header是绑定的,所以我需要使用它 TabControl.ItemTemplate而不是TabItem标题
那么我怎样才开始在TabContem的TabControl.ItemTemplate上获得相同的样式
所以我有这个:
<TabItem Header="Tab1" Style="{StaticResource TabStyle}">
</TabItem>
现在我有了这个:
<TabControl.ItemTemplate>
<!-- this is the header template-->
<DataTemplate>
<TextBlock Text="{Binding Person}" />
</DataTemplate>
</TabControl.ItemTemplate>
这是原始TabItem的样式:
<Style x:Key="TabStyle" TargetType="{x:Type TabItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border Padding="3">
<Grid Name ="grid" Height="24">
<Border Name="BorderName"
CornerRadius="12,12,12,12"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" />
<Rectangle Name="TabItemBackgorund"
RadiusX="12"
RadiusY="12"
Fill="{StaticResource TabItemBackgroundBrush}">
</Rectangle>
<Border Name="border"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter Name="TabItemTextbox"
ContentSource="Header"
Margin="6,0,6,0"
TextBlock.Foreground="{StaticResource TabItemBackgroundBrush}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="TabItemBackgorund" Property="Fill" Value="#00bfc2" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.Foreground" Value="#ffffff" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.FontWeight" Value="Bold" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="TabItemBackgorund" Property="Fill" Value="{StaticResource TabItemBackgroundBrush}" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.Foreground" Value="#9494a3" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.FontWeight" Value="Bold" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="TabItemBackgorund" Property="Fill" Value="#00bfc2" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.Foreground" Value="#ffffff" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.FontWeight" Value="Bold" />
</Trigger>
<Trigger SourceName="grid" Property="IsMouseOver" Value="True">
<Setter TargetName="TabItemBackgorund" Property="Fill" Value="#FF4F78" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.Foreground" Value="#ffffff" />
<Setter TargetName="TabItemTextbox" Property="TextBlock.FontWeight" Value="Bold" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:4)
我已经为TabItem Header编写了一个样式。但是因为我的 TabItem标题绑定我需要使用TabControl.ItemTemplate 而不是TabItem标题
而不是TabItem中的Header="Tab1"
,您可以继续使用原始TabItem的样式与Header
Setter
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Header" Value="{Binding Person}"/>
<Setter Property="Template">
和将样式应用于 TabControl.ItemContainerStyle
<TabControl>
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Header" Value="{Binding Person}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
...
</Style>
</TabControl.ItemContainerStyle>
</TabControl>