wpf 如何更改 ContentPresenter 上的 FontFamily?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/401600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:55:31  来源:igfitidea点击:

How do I Change the FontFamily on a ContentPresenter?

wpfxamlcontentpresenterfont-family

提问by Shaun Bowe

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>

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?

如您所见,有两个 ContentPresenter。我希望第一个使用 Tahoma Bold 作为字体而不是默认的 Tahoma。我怎样才能做到这一点?

回答by Matt Hamilton

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.FontWeightattached property to tell the ContentPresenter that any text inside it should be bold.

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

Try this:

尝试这个:

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

回答by Tallmaris

I can't help about Silverlight, but in the new WPF 4 it is TextElementrather than TextBlock

我对 Silverlight 无能为力,但在新的 WPF 4 中,它是TextElement而不是 TextBlock