XAML cannot find Resources file

Miguel Ángel García Milla 21 Reputation points
2021-11-22T11:56:37.593+00:00

Hello, I have a WPF proyect and I am trying to access the Resources.resx file form a Style element to access its resources and use them. But each time I reference them, it only shows the Settings but no Resources. I have changed Resources.resx to public and setted it's Build Action as an Embedded Resource.
Here is my code:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:resx="clr-namespace:ProjectName.Properties">    
    <Style x:Key="lbl_demo" TargetType="{x:Type Label}">
        <Setter Property="FontFamily" Value="{x:Static resx:Resources.resourceName}"/>
    </Style>    
</ResourceDictionary>
Developer technologies | Windows Presentation Foundation
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-11-23T06:27:31.113+00:00

    If your font resource file is a file in .ttf(.otf) format, you could try to use the resource font by following the steps below.

    1.Adding a font file to Resources.resx(the circled part will automatically appear) and rename the resource to Pacifico.
    151528-image.png
    Project and resource path structure:
    151589-image.png
    2.Right click on the .ttf file, select Properties and set Build Action to Resource:
    151665-image.png
    Dictionary1.xaml:

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

    App.xaml:

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

    MainWindow.xaml:

    <Window.Resources>  
            <Style x:Key="font" TargetType="Label">  
                <Setter Property="FontFamily" Value="Resources/#Pacifico"/>  
            </Style>  
        </Window.Resources>  
        <StackPanel>  
            <Label Style="{DynamicResource font}" Content="hello"/>  
            <Label Style="{StaticResource lbl_demo}" Content="hello"/>  
        </StackPanel>  
    

    Result:
    151656-image.png


    If the answer is the right solution, please click Accept Answer and kindly upvote it. If you have extra questions about this answer, please click Comment. 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.