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

cos*_*smo 4 c# wpf

我正在尝试在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>
Run Code Online (Sandbox Code Playgroud)

现在的问题是它只显示"我的文件夹"并且fontawesome图标消失了 - 我可以确认该图标有效(当我删除它显示的文本块时).

Evk*_*Evk 6

这条线

fa:Awesome.Content="Folder"
Run Code Online (Sandbox Code Playgroud)

通过设置ContentControl.Content目标图标字符来工作.因此它设置Button.Content为代表您的案例中的文件夹图标的字符.但是在下一个语句中,您Button.Content使用新值覆盖(将其设置为StackPanel),因此图标消失了.如果您想要带有图标和文本的按钮 - 在StackPanel中再创建一个文本块并将其用于图标,然后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>
Run Code Online (Sandbox Code Playgroud)