WPF - Add Custom Font - wpf

I'm trying to add a custom font as a resource in my application.
I have a "CustomFont" directory in the application and all the fonts inside of it are set to "Resource"
<Window.Resources>
<Style x:Key="Gotham-XLight">
<Setter Property="TextElement.FontFamily"
Value="/CustomFonts;Component/#Gotham-XLight" />
</Style>
</Window.Resources>
And then on my TextBlock I have this: (inside a grid)
<TextBlock x:Name="TimeTextBlock" Style="{DynamicResource Gotham-XLight}"
TextAlignment="Center" FontSize="25" FontWeight="Bold"
Foreground="White" Text="TextBlockTimer"
Margin="105,242.974,0,226.975" HorizontalAlignment="Left"
Width="221.919" />
But I'm not seeing my font as people say. Am I doing something wrong?

You may want to check the name of the font, you need to specify the name of the font not the name of the file.
Double click on the font file and it should show a "Font name:" that's what you want to make sure is specified in your style.

Try this
<Window.Resources>
<Style x:Key="Gotham-XLight">
<Setter Property="TextElement.FontFamily" Value="CustomFonts/#Gotham-XLight" />
</Style>
</Window.Resources>
Also, if you are not planning on changing the style at runtime {StaticResource Gotham-XLight} will be much more performant.

In xaml I did it like this:
<Button Grid.Column="1" Grid.RowSpan="2" Name="start" Margin="5" Click="start_Click">
<TextBlock Name="test" FontFamily="pack://application:,,,/Y_Yoga;Component/Resources/#FontAwesome"></TextBlock>
</Button>
However, I don't know if #FontAwesome is font's embedded name or is it the result that I renamed the .ttf file.
Hope to be helpful!

Late reply but worth mentioning. To add a custom font that will apply globally in your window you could add this in your csproj file to include the fonts from the Fonts folder of your project as resources.
<ItemGroup>
<Resource Include="Fonts\*.ttf" />
</ItemGroup>
Then in your window XAML you can specify the FontFamily in the Window part:
<Window x:Class="Namespace.MainWindow"
...
FontFamily="/Fonts/#[FONT NAME]"
Title="">
<Grid>
...
</Grid>
</Window>
I hope this could help somebody, as I spent some time to figure it out.

Related

Unable to import font into WPF application

I'm trying to add a custom font to my application, but I'm getting an error:
An object of the type "System.Windows.Style" cannot be applied to a property that expects the type "System.Windows.Media.FontFamily".
From the error, I'm guessing that it means, the font wasn't imported, but I'm not sure why it wasn't.
Here's the code in the MainWindow.xaml file:
<Window.Resources>
<Style x:Key="Raleway">
<Setter Property="TextElement.FontFamily" Value="fonts/#Raleway"/>
</Style>
</Window.Resources>
<TextBlock Text="text"
FontWeight="Light"
FontFamily="{StaticResource Raleway}"
FontSize="22"
Foreground="White"
HorizontalAlignment="Center"
/>
This is my directory:
The following markup shows how to reference the application's font resources:
<Window.Resources>
<FontFamily x:Key="Raleway">./fonts/#Raleway</FontFamily>
</Window.Resources>
<TextBlock Text="text"
FontWeight="Light"
FontFamily="{StaticResource Raleway}"
FontSize="22"
Foreground="White"
HorizontalAlignment="Center"
/>
When you add fonts as resources to your application, make sure you are setting the <Resource> element, and not the <EmbeddedResource> element in your application's project file. The <EmbeddedResource> element for the build action is not supported.
It is possible to package fonts with WPF application. The detailed information is here: Packaging Fonts with Applications

Cannot see FontAwesome Icons on Parent Window

Summary -- I cannot see FontAwesome icons in the designer when looking at my MainWindow.xaml view that includes a control that has the FontAwesome icons in it.
We are using Visual Studio 2012 to develop a WPF application using Simple MVVM. In our application, we use FontAwesome for most of our icons. I created a resources library that has a reference to FontAwesome (AppStyles.xaml).
Here is an excerpt from my AppStyles.xaml file that references FontAwesome.
<Style x:Key="FontAwesome" TargetType="Control">
<Setter Property="FontFamily" Value="/Fonts/#FontAwesome" />
</Style>
I have created a HeaderView.xaml view that shows a toolbar of options the user can select from. Example of one of the buttons the user can click is below.
<Button Command="{Binding ShowStartScreenAction}">
<Button.Template>
<ControlTemplate>
<StackPanel Orientation="Horizontal" Margin="0">
<Label Content="" Style="{StaticResource FontAwesomeLabel}" />
<Label Content="Home" Style="{StaticResource ButtonLabel}" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
On the HeaderView.xaml view, the FontAwesome icons show up correctly (). A house in this case.
In my MainWindow.xaml view, I have the HeaderView.xaml view added as a control.
<ctrls:HeaderView Grid.Row="0" Grid.ColumnSpan="2" />
The problem I am having is that the FontAwesome does not show up correctly on MainWindow when I view it in the Designer, so I cannot see how the HeaderView.xaml and all my other controls look together inside MainWindow.
Here is my reference in MainWindow.xaml to my resources library where the FontAwesome reference is.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources;component/Styles/AppStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
The application runs fine, but it would be nice to be able to see what I have in the designer without having to run the application to see it every time. Can someone help?

Silverlight custom Font not working at runtime

I have a Silverlight 4 Application that I run using Visual Studio 2012 Web Express. I added a CustomFont.zip in the Fonts folder of my project. the file is marked as "Resource".
When I use the font, in a TextBlock for example, I can see it in the Visual Studio XAML designer. But when I run the application it uses de default standard font.
Here's an example of my TextBlock
<TextBlock FontFamily="/Fonts/CustomFont.zip#MyFontName" >Hello World</TextBlock>
I have tried with embedded Zip like the example above. I also tried the font directly (without zip). Tried with TTF and OTF Font. Always same result I can see it at design time but not at runtime.
Thanks
I'm sure you have this working by now but I had the same problem. I got it to work by formatting my FontFamily string like below and you definitely don't want the file to be in a zip file. I have my font in a project folder I created called "Fonts".
<TextBlock FontFamily="./Fonts/MyFontName.ttf#MyFontName" >Hello World</TextBlock>
I also had same problem and I solved like below. I had to go through a lot of forums including this but none works. But some forum asked me to forcibly load font before I come to point of my interest.
In my TextBlockStyles.xaml resource dictionary I created a styke
<Style x:Key="RadWindowTitleTextStyle"
TargetType="TextBlock">
<Setter Property="Foreground"
Value="#FFB9D449" />
<Setter Property="FontSize"
Value="18.667" />
<Setter Property="FontFamily"
Value="/sampleawebsite.application;component/Assets/Fonts/UBSHead.ttf#UBSHeadline" />
</Style>
In my MainPage, I just gave below to load my font
<TextBlock Text="Do not display Font load purpose only"
Width="1"
Height="1"
Style="{StaticResource RadWindowTitleTextStyle}"
Foreground="Transparent" />
Below is my point of interest in one of my ChildWindow popup styles resource dictionary called RadWindowStyles.xaml
<TextBlock Text="{TemplateBinding Title}"
Style="{StaticResource RadWindowTitleTextStyle}" />

WP7 Bing Maps Pushpin template with image doesn't show up

My ultimate goal is to have pushpins with custom images on a Bing Map in a WP7 app. I have created a control template and a map with a pushpin. Right now, I can get the default pushpins to show up, but nothing shows when I try to template it. Here's what I have right now:
<phone:PhoneApplicationPage.Resources>
<ControlTemplate x:Key="PushpinControlTemplate" TargetType="my:Pushpin">
<Image Source="/Images/Pins/pin.png" />
</ControlTemplate>
</phone:PhoneApplicationPage.Resources>
<my:Map Name="mapMain" CredentialsProvider="CredKey">
<my:Pushpin/>
</my:Map>
If I apply the PushpinControl template nothing shows up:
<my:Pushpin Template="{StaticResource BoaPushpinControlTemplate}" />
If I remove the template, it shows the default black shape.
I must be doing my template incorrectly, but I don't know what the problem is. Can I not have an image in the ControlTemplate?
If you arent using ItemSource binding on the Map then use simple content control approach
<maps:Pushpin Location="{Binding Location}">
<Image Source="/Images/Pins/pin.png" />
</maps:Pushpin>
Or if you dynamically populating the push-pins use the below approach
<maps:Map x:Name="map" >
<maps:MapItemsControl ItemsSource="{Binding Collection}">
<maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<maps:Pushpin Location="{Binding Location}">
<Image Source="/Images/Pins/pin.png" />
</maps:Pushpin>
</DataTemplate>
</maps:MapItemsControl.ItemTemplate>
</maps:MapItemsControl>
</maps:Map>
Even though this thread is a little bit old I'm going to post my suggestion:
Try out this link Working with Pushpins, it is working for me (create a new style and use it in pushpin declaration)
(App.xaml, do not forget the namespace!)
xmlns:m="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps" >
<Application.Resources>
<Style TargetType="m:Pushpin" x:Key="PushpinStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="m:Pushpin">
<Image Width="24" Height="24" Source="path_to_pic" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(in the xaml there the map is)
<Grid x:Name="LayoutRoot" Background="Transparent">
<m:Map x:Name="Map" Mode="Aerial"
CredentialsProvider="CredKey">
<m:MapItemsControl x:Name="Content">
<m:MapItemsControl.ItemTemplate>
<DataTemplate>
<m:Pushpin Location="{Binding Location}" Style="{StaticResource PushpinStyle}" />
</DataTemplate>
</m:MapItemsControl.ItemTemplate>
</m:MapItemsControl>
</m:Map>
</Grid>
If this isn't working check if the Build Action of your picture is set to content.
Took me a while to figure this out, so i hope i could help someone, despite the fact that this thread is old. ;)

How to use an image defined as an XAML resource as an image control?

Let's say we have the image defined as below.
<Grid.Resources>
<Image x:Key="SomeKey" Source="Path\To\The\Image.png" />
</Grid.Resources>
I want to use it as an image control somewhere. I try:
<Image Source="{StaticeResource SomeKey}" />
But that doesn't work.
You should use it as
<ContentControl Content="{DynamicResource SomeKey}" />
Hope that helps!
ps. Note that we must use DynamicResource instead of StaticResource so that if the image is used many times, a new/separate cloned image is created for each of those cases.

Resources