使用默认样式
<Style TargetType="{x:Type TabItem}"x:Key="HeaderStyleS">
<Setter Property="Header" Value="{Binding RelativeSource={RelativeSource Self},
Path=Content.DataContext.ViewName}" />
</Style>
有效。但在我的情况下,我需要自定义TabItem
所以我用
<Style TargetType="{x:Type TabItem}" x:Key="HeaderStyle">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="true" Height="23">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<ContentPresenter ContentSource="Header" Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center">
<ContentPresenter.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource Foreground}"/>
<Setter Property="Text" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.DataContext.ViewName}"></Setter>
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
<Button Grid.Column="1" Height="15" Width="15" HorizontalAlignment="Center" VerticalAlignment="Center">
...
</Style>
现在的问题是Header
文字未显示。有什么想法吗?
答案 0 :(得分:0)
试试这些:
{Binding Path=Content.DataContext.ViewName, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}
或
{Binding Path=DataContext.ViewName, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}
或
{Binding Path=ViewName}