0

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>
voyboy
  • 11
  • 4
  • The font name is Familiada, not Familiada Regular: `pack://application:,,,/Fonts/#Familiada` – Clemens Aug 04 '23 at 14:29
  • I've changed it as you wrote, but the effect is still the same. Designer is fine, running app not. – voyboy Aug 04 '23 at 19:53
  • Go through the steps in the answer to the duplicate question. What you are showing here should just work (with the correct Pack URI). – Clemens Aug 04 '23 at 20:01
  • Tested this again in a .NET 7 application, and `pack://application:,,,/Fonts/#Familiada Regular` also works well. – Clemens Aug 04 '23 at 20:11
  • I've followed those steps, but it still doesn't work properly. I don't know if it matters but I am trying to use those fonts in ChildWindow which is created inside MainWindow. – voyboy Aug 05 '23 at 07:57
  • When I try to set font of an element by adding FontFamily="{StaticResource Familiada}" I get exception that it cannot find resource called "Familiada". – voyboy Aug 07 '23 at 00:45
  • You mean in the child window? Perhaps show the relevant parts of your code. – Clemens Aug 07 '23 at 06:13
  • I have edited post and added a code blocks. – voyboy Aug 08 '23 at 21:31
  • That does all work fine. No idea where else the error might be. – Clemens Aug 09 '23 at 05:20
  • I've tried also to add Image to ChildWindow, but the result is the same. It's visible in designer but not when the app is running. – voyboy Aug 09 '23 at 07:16
  • We can't tell what's wrong. What you are showing here just works, assuming ChildWindow is in the same project as App.xaml. – Clemens Aug 09 '23 at 08:21
  • Would you mind taking a look on github repository? I'd be really grateful. https://github.com/wojmle/FamilyFeud – voyboy Aug 09 '23 at 18:55

0 Answers0