1

全局定义的灰色文本块

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="{StaticResource BR_SE_Gray}" />
    <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>

我想在下面提到的代码中的网格视图标题单元格中使用黑色。我想要上面代码块中前景属性中的黑色。全局定义的文本块颜色为灰色。如何在下面的代码中覆盖它并使其变黑

<ContentControl x:Name="ContentPresenter"
    Grid.Column="0"
    Margin="{TemplateBinding Padding}"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
    Content="{TemplateBinding Content}"
    ContentTemplate="{TemplateBinding ContentTemplate}"
    FontFamily="pack://application:,,,/Themes/#Arial Rounded MT Bold"
    FontWeight="Bold"
    Foreground="{TemplateBinding Foreground}"
    IsTabStop="{TemplateBinding IsTabStop}">

    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Setter Property="FontSize" Value="8" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontFamily" Value="pack://application:,,,/Themes/#Arial Rounded MT Bold" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </ContentControl.Style>

4

1 回答 1

1

资源是resolved by traversing up logical tree. 因此,您可以override resource通过在 ContentControl 的资源部分下为 textBlock 指定资源并设置ForegroundBlack那里。

这样,new style will get applied to all the textBlocks falling under ContentControlContentControl 之外的文本框将继续使用您的全局样式。

确保您设置BasedOn了样式,以便您的样式inherit other properties set in base style-

<ContentControl>
   <ContentControl.Resources>
      <Style TargetType="{x:Type TextBlock}"
             BasedOn="{StaticResource {x:Type TextBlock}}">
          <Setter Property="Foreground" Value="Black" />
      </Style>
   </ContentControl.Resources>
</ContentControl>
于 2013-10-20T09:39:11.680 回答