WPF - Styles declared in Resource Dictionary not getting applied

Srinivas AK 21 Reputation points
2022-07-19T04:33:42.81+00:00

I have added the following styles in App.xml file of WPF application under Application.Resources tag and these styles are getting applied to the controls.

<Style TargetType="{x:Type TextBlock}">  
  <Setter Property="FontSize" Value="16" />  
  <Setter Property="FontFamily" Value="Calibri" />  
</Style>  
<Style TargetType="{x:Type Control}" x:Key="fontStyling">  
  <Setter Property="FontSize" Value="16" />  
  <Setter Property="FontFamily" Value="Calibri" />  
</Style>  
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource fontStyling}" />  
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource fontStyling}" />  

Now, i have created a "Resource Dictionary" file, moved the above styles to Resource dictionary file and accessed the "Resource Dictionary" file in the App.XAML file using merged dictionaries

<ResourceDictionary x:Key="GlobalSettingsDictionary">  
  <ResourceDictionary.MergedDictionaries>  
    <ResourceDictionary Source="ResourceDictionaries/GlobalSettings.xaml" />  
  </ResourceDictionary.MergedDictionaries>  
</ResourceDictionary>  

When i run the WPF application then the Styles in the resource dictionary are not getting applied to the controls present in the Main Window. When i use the same styles in App.xaml file then they are getting applied

Am i missing anything? Can you please suggest on what needs to be done to get the styles defined in the Resource dictionary are applied to the controls in the MainWindow.xaml?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,341 Reputation points Microsoft Vendor
    2022-07-19T08:03:38.773+00:00

    Hi,@Srinivas AK . Welcome Microsoft Q&A.
    You could refer to the following code to use ResourceDictionary . You can also refer here for more details.
    ResourceDictionary1.xaml:

        <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
            <Style TargetType="{x:Type TextBlock}" x:Key="textBlockStyle">  
            <Setter Property="FontSize" Value="26" />  
            <Setter Property="FontFamily" Value="Calibri" />  
          
            </Style>  
            <Style TargetType="{x:Type Control}" x:Key="fontStyling">  
                <Setter Property="FontSize" Value="16" />  
                <Setter Property="FontFamily" Value="Calibri" />  
            </Style>  
            <Style TargetType="{x:Type Label}" BasedOn="{StaticResource fontStyling}" />  
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource fontStyling}" />  
        
        </ResourceDictionary>  
    

    App.xaml:

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

    MainWindow.xaml:

          <TextBlock x:Name="tb" Width="100" Height="60" Style="{StaticResource ResourceKey=textBlockStyle}" Text="hello"/>  
         <TextBlock x:Name="tb1" Width="100" Height="60"  Text="hello"/>  
         <TextBox x:Name="tb2" Width="100" Height="60"  Text="hello"/>  
         <Label x:Name="lb" Width="100" Height="60"  Content ="hello"/>  
    

    The result:
    222276-image.png

    Update:

    TextBlock is not derived from Control, but directly from FrameworkElement. There is no common class between TextBlock and Control that has FontSize and FontFamily. They both implement it separately. What you can do to create styles for FrameworkElement, set attached properties TextElement.FontSize and TextElement.FontFamily

     <Style TargetType="{x:Type FrameworkElement}" x:Key="fontStyling">  
            <Setter Property="TextElement.FontSize" Value="16" />  
            <Setter Property="TextElement.FontFamily" Value="Calibri" />  
            <Setter Property="TextElement.Foreground" Value="Red" />  
        </Style>  
        <Style TargetType="{x:Type Label}" BasedOn="{StaticResource fontStyling}" />  
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource fontStyling}" />  
        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource fontStyling}" />  
    

    MainWindow:

    <TextBlock x:Name="tb1" Width="100" Height="60"  Text="hello"/>  
            <TextBox x:Name="tb2" Width="100" Height="60"  Text="hello"/>  
            <Label x:Name="lb" Width="100" Height="60"  Content ="hello"/>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    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.


0 additional answers

Sort by: Most helpful