16

I am making use of Font Awesome's icons to render basic font images within my C# WPF application. During run-time when I attempt to alter the TextBlock to display a different font icon but the unicode representation is displayed instead of the font icon.

I have created a sample application to display this. When either of the buttons are clicked, it replaces the TextBlock's Text property with the unicode for the respective icon. There is a Resources folder in the project which has the FontAwesome.ttf font file as a Build Resource which the The TextBlock's FontFamily property references.

Here's my sample application's source code:

Code-Behind:

namespace FontAwesomeTest
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnGlass_Click(object sender, RoutedEventArgs e)
    {
        tblkFontIcon.Text = "";            
    }

    private void btnMusic_Click(object sender, RoutedEventArgs e)
    {
        tblkFontIcon.Text = "";
    }

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        tblkFontIcon.Text = "";
    }        
}
}

XAML:

<Window x:Class="FontAwesomeTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Font Awesome Test Window" Height="300" Width="330" Name="FontAwesomeTestWindow">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="25"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Name="btnGlass" Height="35" Width="85" Click="btnGlass_Click" >Glass</Button>
    <Button Name="btnMusic" Grid.Column="1" Height="35" Width="85" Click="btnMusic_Click">Music</Button>
    <Button Name="btnSearch" Grid.Column="2" Width="85" Height="35"  Click="btnSearch_Click">Search</Button>
    <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontFamily="../Resources/#FontAwesome">&#xf000;</TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontFamily="../Resources/#FontAwesome">&#xf001;</TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontFamily="../Resources/#FontAwesome">&#xf002;</TextBlock>
    <TextBlock Name="tblkFontIcon" Grid.Row="2" Grid.ColumnSpan="3" FontSize="64" FontFamily="../Resources/#FontAwesome" HorizontalAlignment="Center" VerticalAlignment="Center">&#xf011;</TextBlock>
</Grid>

I used the following tutorial to incorporate Font Awesome into my application http://www.codeproject.com/Tips/634540/Using-Font-Icons

So in essence, how can I change the Icon but have an Icon display - not Unicode?

Thanks in advance.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Gareth
  • 243
  • 1
  • 5
  • 12

3 Answers3

30

Font Awesome has NuGet packages named FontAwesome.UWP and FontAwesome.WPF. Just download one of this.

NuGet Packages for Font Awesome

If you will use a icon import follow namespace into your XAML code:

xmlns:fa="http://schemas.fontawesome.io/icons/"

Use it into your button like this:

<Button x:Name="btnButton">
    <Button.Content>
        <fa:ImageAwesome Icon="LongArrowLeft"/>
    </Button.Content>
</Button>

And finally in your C# code behind:

using FontAwesome.WPF; // on the top of the code
btnButton.Content = FontAwesomeIcon.LongArrowRight;
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • 3
    This is a very nice way how it becomes Handy to use fontawesome. – LuckyLikey Mar 28 '17 at 07:48
  • 1
    For the same XAML, I had to use the following code behind: `(btnButton.Content as ImageAwesome).Icon = FontAwesomeIcon.LongArrowRight;` – Matt Nov 11 '17 at 06:24
  • Thank you, very helpful steps. What all other posts left out was "If you will use a icon import follow namespace into your XAML code:". or your markup would not build/compile. – OverMars Feb 01 '18 at 02:30
  • How do you set image sources when using this package? – Paul Swetz Apr 19 '18 at 17:03
  • @paul what do you mean? It's an icon font. – H. Pauwelyn Apr 19 '18 at 18:40
  • The answers above show how to use the base font as an image source which is something I need to do a lot. I would assume there is some way to accomplish this with the nugget package but I cannot figure out how to do it. It is pretty rare that I actually get to use the XAML controls from font awesome. – Paul Swetz Apr 20 '18 at 15:03
  • @paul: It's indeed a XAML control. But in code behind of the nuget it's possible that it's using an `Image` control. It's also mutch easier to use the FontAwesome XAML controls. – H. Pauwelyn Apr 20 '18 at 15:59
21

UPDATE

I found a different post for this topic -- Add Icon font in wpf I think this should be more likely to what you want.

Make sure your font is added as a resource. Then, use the following string:

<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/fonts/#FontAwesome" />

In the string above, I'm assuming that the font's name (not the font's filename) is FontAwesome.

You just need to:

  1. Add the Font to your project, let's say you put them in to a folder "fonts"
  2. Change the Build Action to Resource not Embedded Resource
  3. Add your style to set the font family like the code snip above, and set the TextBlock.Text to the icon you like and apply the style to the TextBlock.

If you want change the icon by updating the TextBlock.Text property, you should set the Text property with the supported unicode string.

Try something like

 tblkFontIcon.Text = "\uf000";

rather than

tblkFontIcon.Text = "&#xf000;";

If your're using the code from Using Font Icons

then you probably missed the "How It Works" section in that post. You should use that markup extension, rather than using the TextBlock.Text property.

In his sample code:

<RibbonButton Label="Import data" 
  LargeImageSource="{WpfTools:ImageFromFont Text=&#xf01a;, 
  FontFamily=/RibbonFontIconSample;component/Resources/#FontAwesome, Brush=Brown}" />

pay attention to the WpfTools:ImageFromFont, it is the Markup Extention, it allows xaml parser to convert the

{WpfTools:ImageFromFont Text=&#xf01a;, 
      FontFamily=/RibbonFontIconSample;component/Resources/#FontAwesome, Brush=Brown}

to an ImageSource and assigned to the LargeImageSource property.

So in your xaml, you could replace the TextBlock with an Image, then it should be something like:

<Image Source="{WpfTools:ImageFromFont Text=&#xf000;, 
      FontFamily=/RibbonFontIconSample;component/Resources/#FontAwesome, Brush=Brown}" />

If you want to change the Icon, you will have to change the ImageSource yourself, just follow the Using Font Icons to create your own method, or simply copy the following code from that tutorial.

private static ImageSource CreateGlyph(string text, 
        FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, 
        FontStretch fontStretch, Brush foreBrush)
{
    if (fontFamily != null && !String.IsNullOrEmpty(text))
    {
        Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
        GlyphTypeface glyphTypeface;
        if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                throw new InvalidOperationException("No glyphtypeface found");

        ushort[] glyphIndexes = new ushort[text.Length];
        double[] advanceWidths = new double[text.Length];
        for (int n = 0; n < text.Length; n++)
        {
            ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
            glyphIndexes[n] = glyphIndex;
            double width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
            advanceWidths[n] = width;
        }

        GlyphRun gr = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes,
                                    new Point(0, 0), advanceWidths, 
                                    null, null, null, null, null, null);
        GlyphRunDrawing glyphRunDrawing = new GlyphRunDrawing(foreBrush, gr);
        return new DrawingImage(glyphRunDrawing);

    }
    return null;
}
Community
  • 1
  • 1
terry
  • 1,569
  • 11
  • 19
  • Firstly - thanks for the speedy response! The Update you provided is how I am currently using Fonts in my WPF application. As the question states, it currently works but displays Unicode when I change the Text property even if I change it to the same Unicode. I tried to include images of this but don't have enough 'street cred'. As for the Using Font Icons part, I can't figure out how to use it for changing the Icon at Runtime as my need requires. I can get it to work during Design/Build time. – Gareth Apr 16 '14 at 12:37
  • I think you should set the text with a unicode string rather than set it with "". – terry Apr 16 '14 at 13:05
  • I've updated, and you could follow that code to change your TextBlock.Text to another unicode string, and that should work. – terry Apr 16 '14 at 13:09
  • 2
    because "" is an xaml "version" unicode, the xaml parser translates it into the corresponding unicode string which is "\uf000" in c# and set that to the `tblkFontIcon.Text`. – terry Apr 16 '14 at 13:19
1

Simple and very easy:

You should install fontawesome font on your system use it as follows

<Button Content="&#xf0e4;" FontFamily="FontAwesome" FontSize="32" />

How to compose icon, {&#x}{FontAwesome icon code} e.g &#xf2b9 for address book

To find the list of codes goto FontAwesome or astronaut web

Kolajo
  • 147
  • 1
  • 4