2013-10-09 91 views
1

我在我的應用程序中有一個資源字典,其中它們是爲textblock定義的通用樣式,此字典與app.xaml合併。嵌套控件中的重寫隱式樣式

現在,我已經在那裏我需要改變的TabItem的風格在我的對話窗口,並根據一些觸發器設置前景色的要求,我已經定義我自己的文字塊的風格和寫作模板的TabItem和文本塊爲如下─

/* wriiten in common style */ 
     <Style TargetType="{x:Type TextBlock}"> 
       <Setter Property="Foreground" Value="{StaticResource BR_SE_Black}" /> 
       <Setter Property="TextTrimming" Value="CharacterEllipsis"/> 
       <Setter Property="FontSize" Value="11"/> 
       <Setter Property="FontFamily" Value="Arial Unicode MS"/> 
       <Style.Triggers> 
        <Trigger Property="controls:TextBlockService.IsTextTrimmed" Value="True"> 
         <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 




    /* written in dialog winow */ 
<Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="VerticalAlignment" Value="Center"></Setter> 
      <Setter Property="TextTrimming" Value="CharacterEllipsis"/> 
      <Setter Property="FontSize" Value="11"/> 
      <Setter Property="FontFamily" Value="Arial Unicode MS"/> 
      <Style.Triggers> 
       <Trigger Property="Controls:TextBlockService.IsTextTrimmed" Value="True"> 
        <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 

     <Style TargetType="{x:Type TabControl}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TabControl}" > 
         <Grid KeyboardNavigation.TabNavigation="Local"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="*"/> 
          </Grid.RowDefinitions> 
          <TabPanel Name="HeaderPanel" Grid.Row="0" Panel.ZIndex="1" Margin="0,0,0,-1" IsItemsHost="True" 
               KeyboardNavigation.TabIndex="1" Background="Green" /> 
          <Border Name="Border" Grid.Row="1" Background="#FFFFFF" BorderBrush="#888888" BorderThickness="1" 
              KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.DirectionalNavigation="Contained" KeyboardNavigation.TabIndex="2" > 
           <ContentPresenter Name="PART_SelectedContentHost" Margin="4" ContentSource="SelectedContent" /> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

     <!-- SimpleStyles: TabItem --> 
     <Style TargetType="{x:Type TabItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TabItem}"> 
         <Grid> 
          <Border Name="Border" Margin="0,0,-4,0" Background="Green" CornerRadius="3" BorderBrush="Transparent" BorderThickness="1,1,1,1" > 
           <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" 
                  ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True" /> 
          </Border> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="True"> 
           <Setter Property="Panel.ZIndex" Value="100" /> 
           <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" /> 

           <Setter TargetName="Border" Property="Background" Value="White" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="Foreground" Value="white"></Setter> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Foreground" Value="Green"></Setter> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 

我現在面臨的問題是我試圖在tabitem標題的tabitem模板中設置的前景顏色沒有得到應用,並且它正在採用基本文本塊樣式中定義的顏色,如果我評論前景顏色,則爲 設置基本的文本塊樣式一切工作正常,任何想法爲什麼這個問題正在發生,以及如何解決它?

+0

如果在應用程序資源中定義了隱式TextBlock樣式,則不能在Window資源下隱式**向下覆蓋樹,因爲[TextBlock](https://msdn.microsoft.com/en-us/ library/system.windows.controls.textblock(v = vs.110).aspx)不能從控制派生 - 請參閱[這裏的完整說明...](http://stackoverflow.com/a/9166963/742084) – Cel

回答

6

試着改變你的TabItem的ContentPresenter所以它看起來像下面:

<ContentPresenter 
    x:Name="ContentSite" 
    VerticalAlignment="Center" 
    HorizontalAlignment="Center" 
    ContentSource="Header" 
    Margin="12,2,12,2" 
    RecognizesAccessKey="True"> 
    <ContentPresenter.Resources> 
     <Style 
     TargetType="{x:Type TextBlock}" 
     BasedOn="{x:Null}" /> 
     </ContentPresenter.Resources> 
</ContentPresenter> 

通過向ContentPresenter的資源應該可以擋住自定義樣式爲ContentPresenter下的所有文本塊,並使用默認的樣式風格代替。

+0

感謝您的回覆,但這不是幫助 – ankush

+0

查看最新的答案。理論是正確的,只是風格錯了。 – wdavo