Home > Software design >  Font resets to default when I run the app in Visual Studio - WPF
Font resets to default when I run the app in Visual Studio - WPF

Time:01-25

I'm trying to use a custom font in my WPF project. The problem is that when I run the app it shows the default font, even though in the designer I see the font I chose. Can someone explain why this happens?

MainWindow.xaml

<StackPanel Orientation="Vertical">

    <TextBlock
        Text="SEARCH"
        FontSize="26" 
        FontStyle="Normal" 
        FontWeight="Normal" 
        Foreground="#ffffff"
        Style="{StaticResource MontserratFS}"/>

    <TextBlock
        Text="SEARCH"
        FontSize="26" 
        FontStyle="Normal" 
        FontWeight="Normal"
        Foreground="#ffffff"
        FontFamily="/Fonts/#Montserrat"/>

</StackPanel>

App.xaml

<Application x:Class="Project001.App"      
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:Project001"
         StartupUri="MainWindow.xaml">

<Application.Resources>

    <Style x:Key="MontserratFS">
        <Setter Property="TextElement.FontFamily" 
        Value="/Fonts/#Montserrat"/>
    </Style>

    </Application.Resources>
</Application>

enter image description here

What I see in the designer:

enter image description here

What I see when I run the app:

enter image description here

As you can see the font resets to default for some reason.

CodePudding user response:

You could check if your steps are consistent with the following steps.

1.Adding a font file to Resources.resx(the circled part will automatically appear) . enter image description here

Right click on the resource and rename Montserrat-Bold to Montserrat.

Project and resource path structure:

enter image description here

2.Right click on the .ttf file, select Properties and set Build Action to Resource:

enter image description here

3.Use styles in TextBlock and write code like this(two methods):

Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <FontFamily x:Key="Font">pack://application:,,,/Resources/#Montserrat</FontFamily>
    <Style x:Key="tb_demo" TargetType="{x:Type TextBlock}">
        <Setter Property="FontFamily" Value="{StaticResource Font}"/>
    </Style>
</ResourceDictionary>

App.xaml:

 <Application.Resources>
        <ResourceDictionary Source="Dictionary1.xaml"/>
    </Application.Resources>

MainWindow.xaml:

<Window.Resources>
        <Style x:Key="font" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="Resources/#Montserrat"/>
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="SEARCH" Style="{StaticResource tb_demo}" />
        <TextBlock Text="SEARCH" Style="{DynamicResource font }" />
        <TextBlock Text="SEARCH" />
    </StackPanel>

result:

enter image description here enter image description here

  •  Tags:  
  • Related