2014-12-19 3 views
0

Использование по умолчанию стильКак использовать связывающую RelativeSource в стиле

<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

Попробуйте эти:

{Binding Path=Content.DataContext.ViewName, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}

или

{Binding Path=DataContext.ViewName, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}

или

{Binding Path=ViewName}

+0

Это не работает, 'Content' вводится с помощью Prism RegionManager. – baozi

+0

@baozi Задумывались ли вы об использовании 'HeaderTemplate'? –