【问题标题】:How to change Fontawesome.wpf Icon in c# code如何在 C# 代码中更改 Fontawesome.wpf 图标
【发布时间】:2020-05-28 13:00:35
【问题描述】:

我对 wpf 编程相当陌生,所以这可能很基础,但我不知道如何动态更改按钮的 FontAwesome 图标(在 c# 代码中)。我使用本文here 中描述的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;
     }
}

如前所述,单击按钮时它不显示图标,而仅显示文本“停止”,再次按下时“播放”。

【问题讨论】:

    标签: c# wpf font-awesome


    【解决方案1】:

    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 };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2011-05-28
      • 2014-10-07
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      相关资源
      最近更新 更多