Search code examples
wpfcoding-styletextblock

WPF Global style can't be overridden


This global style is declared in App.xaml:

<Style TargetType="TextBlock">
     <Setter Property="FontFamily" Value="Times New Roman"/>
     <Setter Property="FontSize" Value="20"/>
</Style>   

In a window, I've tried to override this locally like this:

<Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
     <Setter Property="FontSize" Value="60" />
</Style>

and this:

<Style TargetType="TextBlock">
     <Setter Property="FontSize" Value="60" />
</Style>

but nothing works. I'm still stuck with the style set up in App.xaml. Does anyone have any idea what could be interfering with this? If I remove that global from App.xaml, I can set whatever I want locally. If I change a value in the global, it's reflected globally, so I don't think there's another global anywhere that's conflicting with it. I searched for TargetType="TextBlock" and got nothing.

Any ideas?


Solution

  • Tried your code and it works perfect for me. Where is your window style placed? I copied it into the Window.Resources and it displays the font with fontsize 60. If this doesn't work try your code in a new empty project.

      <Window.Resources>
        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
          <Setter Property="FontSize" Value="60" />
        </Style>
      </Window.Resources>