在 WPF 中使用 fontawesome 在按钮中添加文本

6
我正在尝试在WPF中的fontawesome图标下生成一些文本:
<Button HorizontalAlignment="Left" Height="40" Width="40" FontFamily="Arial Black" Foreground="White" Margin="5,30,0,0" fa:Awesome.Content="Folder">
    <StackPanel>
        <TextBlock>
            My Folders
        </TextBlock>
    </StackPanel>
</Button>

现在的问题是它只显示“我的文件夹”,而fontawesome图标消失了 - 我可以确认该图标有效(当我删除文本块时,它会显示)。

我猜 fa:Awesome.Content="Folder" 应该在 StackPanel 的某个位置。否则,您期望的对齐方式不清楚。 - Diligent Key Presser
2个回答

10

这一行

fa:Awesome.Content="Folder"

通过将ContentControl.Content设置为目标图标字符来实现。所以在您的情况下,它会将Button.Content设置为表示文件夹图标的字符。但是,在下一条语句中,您会用新值覆盖Button.Content(将其设置为StackPanel),因此图标消失了。如果您想要带有图标和文本的按钮,请在StackPanel中创建一个更多的textblock,并将其用于图标,然后从按钮本身中删除fa:Awesome.Content =“Folder”。或者更好地使用FontAwesome控件而不是TextBlock,像这样:

<Button HorizontalAlignment="Left"
        Height="40"
        Width="300"
        FontFamily="Arial Black"
        Foreground="White"
        Margin="5,30,0,0">
    <StackPanel Orientation="Horizontal">
        <fa:FontAwesome Icon="Folder" Margin="0,0,5,0"/>
        <TextBlock>My Folders</TextBlock>
    </StackPanel>
</Button>

1

Try this out:

<Button HorizontalAlignment="Left" Height="40" Width="40" FontFamily="Arial Black" Foreground="White" Margin="5,30,0,0">
<StackPanel>
    <fa:ImageAwesome Icon="Folder"/>
    <TextBlock>
        My Folders
    </TextBlock>
</StackPanel>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接