如何更改 ContentPresenter 上的 FontFamily? [英] How do I Change the FontFamily on a ContentPresenter?

查看:33
本文介绍了如何更改 ContentPresenter 上的 FontFamily?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展器的自定义模板,它与下面的代码很接近.我不得不更改一些代码以取出自定义类、画笔等.

I have a custom template for an expander that is close to the code below. I had to change some of the code to take out custom classes, brushes, etc..

<Style TargetType="{x:Type Expander}">
  <Setter Property="HorizontalContentAlignment"
          Value="Stretch" />
  <Setter Property="VerticalContentAlignment"
          Value="Top" />
  <Setter Property="BorderBrush"
          Value="Transparent" />
  <Setter Property="FontFamily"
          Value="Tahoma" />
  <Setter Property="FontSize"
          Value="12" />
  <Setter Property="Foreground"
          Value="Black" />
  <Setter Property="BorderThickness"
          Value="1" />
  <Setter Property="Margin"
          Value="2,0,0,0" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Expander}">
        <Border x:Name="Border"
                SnapsToDevicePixels="true"
                Background="White"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Margin="0,0,0,10"
                Padding="0"
                CornerRadius="8">
          <DockPanel>
            <Border x:Name="HeaderSite"
                    Background="Blue"
                    CornerRadius="8"
                    Height="32"
                    DockPanel.Dock="Top">
              <DockPanel>
                <ToggleButton Foreground="White"
                              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                              Margin="0"
                              MinHeight="0"
                              MinWidth="0"
                              Padding="6,2,6,2"
                              IsChecked="{Binding Path=IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                              DockPanel.Dock="Left">
                </ToggleButton>                

                <ContentPresenter SnapsToDevicePixels="True"
                                  HorizontalAlignment="Left"
                                  Margin="4,0,0,0"
                                  ContentSource="Header"
                                  VerticalAlignment="Center"
                                  RecognizesAccessKey="True" />
              </DockPanel>
            </Border>
            <Border x:Name="InnerBorder"
                    Margin="0" >
              <ContentPresenter Focusable="false"
                                Visibility="Collapsed"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                Margin="{TemplateBinding Padding}"
                                x:Name="ExpandSite"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                DockPanel.Dock="Bottom" />
            </Border>
          </DockPanel>
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsExpanded"
                   Value="true">
            <Setter Property="Margin"
                    TargetName="InnerBorder"
                    Value="5" />           
            <Setter Property="Visibility"
                    TargetName="ExpandSite"
                    Value="Visible" />
          </Trigger>
          <Trigger Property="IsEnabled"
                   Value="false">
            <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
          </Trigger>         
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

如您所见,有两个 ContentPresenter.我希望第一个使用 Tahoma Bold 作为字体而不是默认的 Tahoma.我该怎么做?

As you can see there are two ContentPresenters. I would like the first one to use Tahoma Bold as the font instead of the default Tahoma. How can I do this?

推荐答案

您需要使用 FontWeight 属性来指定粗体字体.但是,您可能已经注意到 ContentPresenter 没有该属性.所以你需要使用 TextBlock.FontWeight 附加属性告诉 ContentPresenter 里面的任何文本都应该是粗体.

You need to use the FontWeight property to specify a bold font. However, you've probably noticed that ContentPresenter doesn't have that property. So you'll need to use the TextBlock.FontWeight attached property to tell the ContentPresenter that any text inside it should be bold.

试试这个:

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

这篇关于如何更改 ContentPresenter 上的 FontFamily?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆