1

I'm fairly new to programming in wpf, so this might be pretty basic, but I have no Idea how to change a FontAwesome Icon of a button dynamically (in c# code). I use the nuget package as described in this post here. However the last part:

And finally in your C# code behind:

using FontAwesome.WPF; // on the top of the code
btnButton.Content = FontAwesomeIcon.LongArrowRight;

is not working for me, as it only shows the text "LongArrowRight" after clicking the button.

This is my code:

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

As already stated when the button is clicked it does not show the Icon but only the text "Stop" and when pressed again "Play".

KNST
  • 28
  • 5

1 Answers1

1

FontAwesomeIcon is enum. When you assign enum value to Button.Content it will be displayed as text.

You need to assign ImageAwesome element to Content:

btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.LongArrowRight };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Play };
btnPlay.Content = new ImageAwesome { Icon = FontAwesomeIcon.Stop };
ASh
  • 34,632
  • 9
  • 60
  • 82