-->

Need to be able to change Font size across an enti

2019-04-16 08:07发布

问题:

I have built a GIS routing application, using WPF, MVVM, and Visual Studio 2015.

When I test the app on a laptop, the fonts seem smaller than when I tested in debug mode on my pc.

Is there a way to create a feature in my application settings so a user could increase the size of all of the fonts, globally, without affecting the graphics? I was hoping this would be similar to a webpage design with the 'ctrl +'.

Any help would be greatly appreciated.

回答1:

I've added similar functionality to my application using ViewBox. Note, it doesn't change the font size, but instead "zooms" all aspects of the user interface. Not sure if this is exactly what you want, but this is what it looks like to give you an idea:

To get this, I wrapped the highest level View (that hosts all content, this could be done on the Window level) in a ViewBox, and then bound the Width and Height to properties in the ViewModel that I could edit using a "zoom" amount:

<Viewbox SnapsToDevicePixels="True" >
    <DockPanel Width="{Binding Width}" Height="{Binding Height}" SnapsToDevicePixels="True">
          ...content...
    </DockPanel>
</ViewBox>

With the width and height:

    private int BaseWidth = 1150;
    private int BaseHeight = 750;

    public int Width
    {
        get
        {
            return (int)(BaseWidth * appSettings.Zoom);
        }
    }

    public int Height
    {
        get
        {
            return (int)(BaseHeight * appSettings.Zoom);
        }
    }

This not only allows the application to be resolution independent (everything wont look tiny on a large DPI screen) it will also allow users to scale if they are having trouble reading things, or if they simply prefer it.

Pretty simple. To effect only font size, you should probably be looking at building style templates for the components. I'd bet you'd catch a lot of the text by simply setting a global TextBlock style with the font size - not sure how dynamic you can be with the default styles.

Edit: Try this with styles:

<sys:Double x:Key="BaseFontSize">12</sys:Double>

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="FontSize" Value="{DynamicResource BaseFontSize}"/>
</Style>

You might have to have a list of wpf controls (buttons, checkboxes, textboxes etc etc) but it wouldn't be too difficult.

BaseFontSize should be able to be changed. All controls without a style set will use this as default if it's in window or app resources. So you won't need to go through all the controls making sure they have a style set.