Search code examples
wpfxamlstyles

xaml styles Is there a way to specify fontsize to one of 5 values?


I would like to be able to support 5 sizes, verysmall, small, normal, large, verylarge.

is there a smart way to do that with styles? < fontsize={DynamicResourec Small}"/>

where I define small somewhere else?

  • I am aware that i could just write 18, insstead, but I might want to scale the fonts later...

Solution

  • Here is a complete example with .Net 5 where the resource is in MainWindow.

    <Window x:Class="WpfNet5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfNet5"
        xmlns:system="clr-namespace:System;assembly=System.Runtime"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <system:Double x:Key="VerySmall">12</system:Double>
        <system:Double x:Key="Small">18</system:Double>
        <system:Double x:Key="Medium">22</system:Double>
        <system:Double x:Key="Large">26</system:Double>
    </Window.Resources>
    
      <Grid>
          <StackPanel >
              <TextBlock Text="Very small" FontSize="{DynamicResource VerySmall}"/>
              <TextBlock Text="Small" FontSize="{DynamicResource Small}"/>
              <TextBlock Text="Medium" FontSize="{DynamicResource Medium}"/>
              <TextBlock Text="Large" FontSize="{DynamicResource Large}"/>
          </StackPanel>
      </Grid>
    </Window>
    

    How it looks in run-time: enter image description here