如何更改cutom标签的前景色,当类型为文本块有全局样式时

本文关键字:文本 全局 样式 类型 cutom 何更改 标签 前景色 | 更新日期: 2023-09-27 18:35:35

现在,我将类型文本块的样式定义为全局资源,换句话说,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>

如何更改cutom标签的前景色,当类型为文本块有全局样式时

解决方案一:样式可以为"标签"添加下边框,并覆盖全局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>

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

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

如何更改内容演示器上的字体系列?