如何更改ContentPresenter上的FontFamily?

我有一个接近下面的代码的扩展器的自定义模板。 我不得不改变一些代码来取出自定义类,画笔等。

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

您需要使用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" /> 

我无法帮助Silverlight,但是在新的WPF 4中,它是TextElement而不是TextBlock

Interesting Posts