0

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...
7
  • 1
    Yes, in your resources and name it x:Key="Small". May 25, 2021 at 13:14
  • ok, that makes sense, but How do I add an int with the name small... probably very simple
    – kfn
    May 25, 2021 at 13:16
  • 1
    FontSize is of type System.Double: <system:Double x:Key="Small" >18</system:Double> May 25, 2021 at 13:19
  • 1
    If you add that to Application.Resouces it will available in your whole app May 25, 2021 at 13:20
  • 1
    What kind of problems? System.Double exists in .Net 5 as well learn.microsoft.com/en-us/dotnet/api/system.double?view=net-5.0 May 25, 2021 at 13:26

1 Answer 1

3

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

1
  • Thanks,. that was exactly what I was looking for, and my trouble was, very simpl. I wrote <System.Double .... thanks ;-)
    – kfn
    May 27, 2021 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.