Search code examples
.netwpfstyles

Set WPF Font size based on screen size


I'd like to have a default font size that is based on the users screen resolution. ie if screen work area width is less than 1024 use font size 10 else use font size 14. Am currently using the "SystemParameters.WorkArea.Width" to determine the width

I can set a global font size and have a converter to return a font size, but am really struggling to work out how to make it fire. ie. when the window size changes, make it check what the font size should be

<Window.Resources>

    <local:SetFontSize x:Key="myConverter" />

    <Style TargetType="{x:Type Control}" x:Key="baseStyle">
        <Setter Property="FontSize" Value="14"  />
        <Setter Property="FontFamily" Value="Calibri" />           

</Window.Resources>

I have found the below event, but just can't seem to link the two together (although I might be going about this wrong way)

<Window.Triggers>
    <EventTrigger RoutedEvent="SizeChanged">          
    </EventTrigger>
</Window.Triggers>

Solution

  • Why would you want this anyway? Wpf uses abstract coordinates, and convert them to pixels using the current Monitor DPI and resolution. So, your application should automatically scale to look similar on every screen. I would suggest, using stretchy layout, with scrollbars, etc. To enabled your app being used on smaller screens.