Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

wpf - How do I Change the FontFamily on a ContentPresenter?

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?

question from:https://stackoverflow.com/questions/401600/how-do-i-change-the-fontfamily-on-a-contentpresenter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...