0
<Window.Resources>
    <Style TargetType="{x:Type TextElement}">
        <Setter Property="FontSize" Value="50"/>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Orientation="Vertical" Grid.Column="0" >

        <Button Click="ButtonBase_OnClick" x:Name="qq">xxx</Button>
        <Button>xxx</Button>
        <Button>xxx</Button>
    </StackPanel>
</Grid>

code like above

as I know, FontSize of Button is a DP from TextElement,but why this code no effect?

 <Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="FontSize" Value="50"/>
    </Style>
</Window.Resources>

if I change the TextElement to Button everything is ok, why ?

lsl
  • 19
  • 3

2 Answers2

0

Unlike Button, TextElement is not a Control. It doesn't inherit from the System.Windows.Control class and has no ControlTemplate and because of this the implicit style doesn't get applied. Please refer to the following links for more information about this.

https://blogs.msdn.microsoft.com/wpfsdk/2009/08/27/implicit-styles-templates-controls-and-frameworkelements/ Implicit styles in Application.Resources vs Window.Resources?

If you want to define a global text style, you could add an implicit TextBlock style to your App.xaml:

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp2"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="FontSize" Value="50"/>
        </Style>
    </Application.Resources>
</Application>
mm8
  • 163,881
  • 10
  • 57
  • 88
0

The problem you ran into is that property inheritance (from the Button control) has a lower precedence than style setters (from the Style in Application).

Have a look at the Dependency Property Value Precedence page on MSDN for a sorted list of all precedences.

To set the global text size you should set FontSize on your MainWindow. That way you can still use inheritance and styling to override the global text size.

Wouter
  • 2,170
  • 1
  • 28
  • 58