当存在类型为TextBlock的全局样式时,如何更改自定义标签字体颜色

时间:2014-12-29 03:37:03

标签: c# wpf xaml label

现在,我将Type TextBlock的样式定义为全局资源,换句话说,TextBlock现在在我的应用程序中有一个默认样式。我想为标签添加底部边框。但我不知道如何根据我的需要更改标签前景中的文本块(例如,我想将其前景颜色设为蓝色)。现在标签的子子TextBlock使用默认样式。

<Style x:TargetType="{x:Type TextBlock}">
   <Setter Property="Foreground" Value="#FFFFF0" /> 
</Style>

<Style x:Key="{x:Type Label}"
   TargetType="Label">
<Setter Property="HorizontalContentAlignment"
      Value="Left" />
<Setter Property="VerticalContentAlignment"
      Value="Top" />
<Setter Property="Template">
<Setter.Value>
  <ControlTemplate TargetType="Label">
    <Border>
      <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        RecognizesAccessKey="True" />
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="IsEnabled"
               Value="false">
        <Setter Property="Foreground">
          <Setter.Value>
            <SolidColorBrush Color="{DynamicResource DisabledForegroundColor}" />
          </Setter.Value>
        </Setter>
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>
</Setter.Value>
 </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

解决方案一: 该样式可以为Label添加底部边框并覆盖全局TextBlock样式。

 <Style TargetType="Label" x:Key="TodoPlaceHolder">
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontFamily" Value="Corbel" />
    <Setter Property="FontSize" Value="18" />
    <Setter Property="Foreground" Value="#F0F0F0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Label">

                <Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                    <TextBlock HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                               Text="{TemplateBinding Content}"
                               Foreground="{TemplateBinding Foreground}"
                               FontFamily="{TemplateBinding FontFamily}"
                               FontSize="{TemplateBinding FontSize}"
                               FontWeight="{TemplateBinding FontWeight}"
                             Margin="{TemplateBinding Margin}">
                    </TextBlock>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

解决方案二: 使用样式替换Label的模板中的TextBlock

<ContentPresenter TextBlock.FontFamily="Tahoma"
              TextBlock.FontWeight="Bold"
              SnapsToDevicePixels="True"
              HorizontalAlignment="Left"
              Margin="4,0,0,0"
              ContentSource="Header"
              VerticalAlignment="Center"
              RecognizesAccessKey="True" />

How do I Change the FontFamily on a ContentPresenter?