3

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

现在我有一个要求,我需要在我的对话框窗口中更改 tabitem 的样式并根据几个触发器设置前景,我已经定义了我自己的 textblock 样式并为 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 标题设置的前景色没有得到应用,并且它采用基本 textblock 样式中定义的颜色,如果我评论基本 textblock 样式中的前景色设置一切正常,知道为什么会出现这个问题,如何解决?

4

1 回答 1

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 下所有文本块的自定义样式,并改用默认样式。

于 2013-10-09T05:04:01.583 回答