覆盖嵌套控件中的隐式样式

时间:2013-10-09 03:59:53

标签: wpf xaml

我的应用程序中有一个资源字典,其中它们是为textblock定义的通用样式,此字典与app.xaml合并。

现在我有一个要求,我需要在对话框窗口中更改tabitem的样式,并根据几个触发器设置前景,我已经为tabitem和textblock定义了我自己的文本块样式和书面模板,如下所示 -

/* 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标头设置未应用,并且它采用基本文本块样式定义的颜色, 如果我评论基本文本块样式中的前景色设置一切正常,任何想法为什么会发生这个问题,以及如何解决?

1 个答案:

答案 0 :(得分:8)

尝试更改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下所有文本块的自定义样式,而是使用默认样式。