I am struggling with adding a new font. I've went through many topics on forum and found many solutions but none of them fixed my problem. I have created WPF app with MVVM logic. The problem is an issue that custom font is visible in xaml designer but in running app it changes to some default font.
I've created folder Fonts and placed my font there. I've made sure that Build Action is set to Resource as well as it is included in csproj file. I've also double checked the name of the font (not file name)
Code in App.xaml look like this:
<Application x:Class="FamilyFeud.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FamilyFeud"
xmlns:view="clr-namespace:FamilyFeud.MVVM.View"
xmlns:viewModel="clr-namespace:FamilyFeud.MVVM.ViewModel">
<Application.Resources>
<FontFamily x:Key="Familiada">pack://application:,,,/Fonts/#Familiada Regular</FontFamily>
</Application.Resources>
</Application>
And style in window is implemented this way:
<Window.Resources>
<Style TargetType ="TextBlock">
<Setter Property ="Background" Value ="Black"/>
<Setter Property ="Foreground" Value ="Yellow"/>
<Setter Property ="FontFamily" Value ="{StaticResource Familiada}"/>
</Style>
</Window.Resources>
EDIT: That's how code looks like: App.xaml.cs:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
var window = new MainWindow();
window.Show();
base.OnStartup(e);
}
}
Then MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
var viewModel = new MainWindowViewModel();
ChildWindow childWindow = new ChildWindow();
childWindow.DataContext = viewModel;
childWindow.Show();
InitializeComponent();
DataContext = viewModel;
}
}
And the fragment of ChildWindow.xaml where I'd like to use custom fonts looks like this:
<Window x:Class="FamilyFeud.MVVM.View.ChildWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FamilyFeud"
mc:Ignorable="d"
Title="ChildWindow" Height="720" Width="1080" Background="Black" >
<Window.Resources>
<Style TargetType ="TextBlock">
<Setter Property ="Background" Value ="Black"/>
<Setter Property ="Foreground" Value ="Yellow"/>
<Setter Property ="FontFamily" Value ="{StaticResource Familiada}"/>
<Setter Property="FontSize" Value ="30"></Setter>
</Style>
</Window.Resources>
.
.
.
</Window>