c# - 如何在 C# 代码中更改 Fontawesome.wpf 图标
问题描述
我对 wpf 编程相当陌生,所以这可能很基本,但我不知道如何动态更改按钮的 FontAwesome 图标(在 c# 代码中)。我在这里使用本文中描述的 nuget 包。然而最后一部分:
最后在你的 C# 代码后面:
using FontAwesome.WPF; // on the top of the code btnButton.Content = FontAwesomeIcon.LongArrowRight;
对我不起作用,因为它仅在单击按钮后显示文本“LongArrowRight”。
这是我的代码:
<Window
...
xmlns:fa="http://schemas.fontawesome.io/icons/">
<Grid>
<Button x:Name="btnPlay" Click="btnPlay_Click">
<Button.Content>
<fa:ImageAwesome Icon="Play"/>
</Button.Content>
</Button>
</Grid>
</Window>
using FontAwesome.WPF; // at the top
private bool running = false;
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
if (running)
{
running = false;
btnPlay.Content = FontAwesomeIcon.Play;
}
else
{
running = true;
btnPlay.Content = FontAwesomeIcon.Stop;
}
}
如前所述,单击按钮时它不显示图标,而仅显示文本“停止”,再次按下“播放”时。
解决方案
FontAwesomeIcon
是枚举。当您将枚举值分配给 Button.Content 时,它将显示为文本。
您需要将ImageAwesome
元素分配给内容:
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.LongArrowRight };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Play };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Stop };
推荐阅读
- jquery - 我试图让这个光标效果反应
- entity-framework - EFCore 2.2.2 过滤相关子实体
- php - 通过 PHP 执行 python 脚本
- c - C - 使用 fgets() 填充通过循环迭代的结构数组。如何在同一行打印值?
- angular - switchMap angular observable 后发出的问题
- android - 从 android studio 编码并在 python raspberry pi 中解码的图片
- c - 使用 C 验证文件中的登录凭据
- javascript - 如何判断 Javascript 对象是否是文件?
- python - tf.image.convert_image_dtype 的错误?值不缩放
- angular - 当用户访问 Ionic 4 中的特定路线时重定向用户