如何更改 ContentPresenter 上的 FontFamily?

发布于 2024-07-11 18:38:20 字数 4025 浏览 14 评论 0原文

我有一个与下面的代码接近的扩展器的自定义模板。 我必须更改一些代码才能取出自定义类、画笔等。

<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。 我怎样才能做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-07-18 18:38:21

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

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

绮烟 2024-07-18 18:38:20

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

尝试这个:

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

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.

Try this:

<ContentPresenter TextBlock.FontFamily="Tahoma"
                  TextBlock.FontWeight="Bold"
                  SnapsToDevicePixels="True"
                  HorizontalAlignment="Left"
                  Margin="4,0,0,0"
                  ContentSource="Header"
                  VerticalAlignment="Center"
                  RecognizesAccessKey="True" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文