1

I need to set FontSize property for all controls in my wpf-layout at once. I mean I don't want to set it for labels, then for chechboxes etc. I want to set it for all controls which support this property.

So, in "Settings" of my module I have values of font size for buttons and for the rest of controls. For buttons I set font size this way:

<Style TargetType="Button">
    <Setter Property="FontSize" Value="{Binding Source={x:Static properties:Settings.Default}, Path=ButtonFontSize}" />
</Style>

Now I need to set FontSize for the rest of controls.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Tomcat
  • 35
  • 5
  • You don't need to set the FontSize of all control types separately. Just set the FontSize property of the Window. Due to property value inhertance, that value is automatically passed to all child elements. – Clemens Feb 05 '19 at 08:56
  • Please do not change your question if it was answered or closed. Ask a new question instead. – Clemens Feb 05 '19 at 08:59
  • It doesn't work, I've already tried to put this in the MainWindowResources.xaml – Tomcat Feb 05 '19 at 13:06
  • How about simply setting the Window's FontSize directly: ``? – Clemens Feb 05 '19 at 13:11
  • My app is MVVM app, based on Josh Smith MVVM example. It has main window and the rest "windows" are user controls, which are hosted by this main window. And I need to give users possibility to choose font size for the whole bunch of "windows". I need to find the way to define it in some single place, because write the same code in each View is insane (I guess). – Tomcat Feb 05 '19 at 13:33
  • Then put the default Window Style in `Application.Resources` in App.xaml. – Clemens Feb 05 '19 at 13:57

3 Answers3

1

You can set it on the window i guess, as it should be inherited from the Parent Control.

<Style TargetType="{x:Type Window}">
     <Setter Property="FontSize" Value="24" />
</Style>
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

Copied from this answer.


I'd do it this way:

<Window.Resources>
        <Style TargetType="{x:Type Control}" x:Key="baseStyle">
            <Setter Property="FontSize" Value="100" />
        </Style>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"></Style>
        <Style TargetType="{x:Type Label}" BasedOn="{StaticResource baseStyle}"></Style>
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource baseStyle}"></Style>
        <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource baseStyle}"></Style>
        <!-- ComboBox, RadioButton, CheckBox, etc... -->
</Window.Resources>
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Looks nice, but what If I have a few views and I need to apply this font settings for all of them? Can I make some "parent view", put your code there, and inherit it in the rest of layouts? – Tomcat Feb 05 '19 at 08:28
  • 2
    Please see [How do we deal with plagiarized answers?](https://meta.stackoverflow.com/q/268629/1136211) – Clemens Feb 05 '19 at 08:52
  • Besides that, this is the wrong way to do it. See the other answers to the duplicate question. – Clemens Feb 05 '19 at 08:57
-1

If you want to set the fontsize for all controls of one specific tipe you could use this

<Window.Resources>
  <Style TargetType="{x:Type TextBox}">
    <Setter Property="FontSize" Value="24" />
  </Style>      
  <Style TargetType="{x:Type Textblock}">
    <Setter Property="FontSize" Value="20" />
  </Style>
</Window.Resources>

If you have many styles and want to keep the variable editable it makes sense to define it above like so:

<Window.Resources>
  <System:Double x:Key="stdFontSize">15</System:Double>

  <Style TargetType="{x:Type TextBox}">
    <Setter Property="FontSize" Value="stdFontSize" />
  </Style>      
  <Style TargetType="{x:Type Textblock}">
    <Setter Property="FontSize" Value="stdFontSize" />
  </Style>
</Window.Resources>
Denis Schaf
  • 2,478
  • 1
  • 8
  • 17