首页 > 解决方案 > WPF - 字体真棒图标未在运行时显示

问题描述

我正在构建 WPF 应用程序,我想使用 Font Awesome 中的图标。我在样式中包含了 Font Awesome,并在我想使用它的字段中指定了一个字体系列。问题是图标在设计窗口中正确显示,但在运行时变为交叉方块。

有我的字体 XAML

<FontFamily x:Key="ArconRegular">pack://application;,,,/Fonts/#Arcon</FontFamily>
    <FontFamily x:Key="ArconRounded">pack://application;,,,/Fonts/#Arcon Rounded-</FontFamily>
    <FontFamily x:Key="FASolid">pack://application;,,,/Fonts/#Font Awesome 5 Free Solid</FontFamily>

    <Style TargetType="{x:Type Control}" x:Key="BaseStyle">
        <Setter Property="FontFamily" Value="{StaticResource ArconRegular}"/>
    </Style>

    <Style TargetType="{x:Type TextBlock}" x:Key="BaseTextBlockStyle">
        <Setter Property="FontFamily" Value="{StaticResource ArconRegular}"/>
    </Style>

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}"/>
    <Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseStyle}"/>
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}"/>
    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseTextBlockStyle}"/>
    <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource BaseStyle}"/>

    <system:String x:Key="FAMinimizeIcon">&#xf2d1;</system:String>
    <system:String x:Key="FAMaximizeIcon">&#xf2d0;</system:String>
    <system:String x:Key="FACloseIcon">&#xf00d;</system:String>

Buttons XAML(我想在按钮上使用图标)

<Style TargetType="{x:Type Button}" x:Key="WindowControlButton" BasedOn="{StaticResource BaseStyle}">
        <Setter Property="FontFamily" Value="{StaticResource FASolid}"/>
        <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Padding" Value="3"/>
        <Setter Property="Foreground" Value="{StaticResource ForegroundMainBrush}"/>
        <Setter Property="LayoutTransform">
            <Setter.Value>
                <ScaleTransform ScaleX="1.5"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}">
                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Content}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{StaticResource BackgroundLightBrush}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

最后是 MainWindow XAML(我想使用它们的一部分)

<!-- Window Buttons -->
<StackPanel Grid.Column="2" Orientation="Horizontal">
    <Button Style="{StaticResource WindowControlButton}" Content="{StaticResource FAMinimizeIcon}" Command="{Binding MinimizeCommand}"/>
    <Button Style="{StaticResource WindowControlButton}" Content="&#xf2d0;" Command="{Binding MaximizeCommand}"/>
    <Button Style="{StaticResource WindowCloseButton}" Content="&#xf00d;" Command="{Binding CloseCommand}"/>
</StackPanel>

如您所见,我尝试在单独的字符串中指定图标代码并使用 StaticResource 但没有任何效果。

标签: c#wpffont-awesome

解决方案


就我而言,它FontFamily="{TemplateBinding FontFamily}"ControlTemplate


推荐阅读