dimanche 10 mai 2015

WPF - Use icon font

I'm trying to include icon font in my wpf application, but my icon never appears and only displays a rectangle.

Projet structure :

MyProject/
  -- Fonts/
      -- myFont.ttf
  -- MyWindow.xaml

MyWindow.xaml

<Label Text="&#xea21;" FontFamily="/Fonts/#myfont" />

"myfont" is the name of the font (the one I see after "Font name" when I double click on the file). The file's Build Action is set as "Resource".

I also tried the following :

Using style

<Window.Resources>
    <Style x:key="MyFont">
        <Setter property="TextElement.FontFamily"/Fonts/#myfont" />
    </Style>
</Window.Resources>

<Label Text="&#xea21;" Style="{StaticResource MyFont}" />

Using pack URI

<Window.Resources>
    <Style x:key="MyFont">
        <Setter property="TextElement.FontFamily" value="v/Fonts/#myfont" />
    </Style>
</Window.Resources>

<Label Text="&#xea21;" Style="{StaticResource MyFont}" />

None of them work. Moreover, the last one (with pack URI) generates an error. Something like an "index out of range error" (I have VS in French so I can't tell what the message is in English).

Already tried to find a solution on the internet, but all I could find was using pack URI, or checking that the name of the font is correct or that Build Action is set to "Resource".

Thank you.

Aucun commentaire:

Enregistrer un commentaire