WPF 全局字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/893428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:27:49  来源:igfitidea点击:

WPF global font size

wpffonts

提问by Jose

I'm creating a WPF app and I would like to know the best way to be able to change the font size for every element in the ui. Do I create a resource dictionary and set Styles to set the font size for all the controls I use?

我正在创建一个 WPF 应用程序,我想知道能够更改 ui 中每个元素的字体大小的最佳方法。我是否创建资源字典并设置样式以设置我使用的所有控件的字体大小?

What is the best practice?

最佳做法是什么?

回答by Carlo

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>

That way, if I want to change ALL the controls, I'd just have to change the "baseStyle" style, the rest would just inherit from it. (That's what BasedOn property those, you can also extend the base style if you create other setters inside of the inherited style)

这样,如果我想更改所有控件,我只需要更改“baseStyle”样式,其余的将继承它。(这就是 BasedOn 属性,如果您在继承的样式内创建其他 setter,您还可以扩展基本样式)

回答by Matze

FontSizeProperty is inherited from Parent Control. So you just need to change FontSize of your main window.

FontSizeProperty 继承自父控件。所以您只需要更改主窗口的 F​​ontSize。

If you don't need dynamic behaviour this should work:

如果您不需要动态行为,这应该可以工作:

Add a style for Window to your ResourceDictionary

将 Window 的样式添加到您的 ResourceDictionary

<Style TargetType="{x:Type Window}">
     <Setter Property="FontSize" Value="15" />
</Style>

Apply the style to your main form (will not be applied implicit because its a derived type)

将样式应用于您的主表单(不会隐式应用,因为它是派生类型)

 Style = (Style)FindResource(typeof (Window));

回答by Janaka R Rajapaksha

<Window>has a property FontSize.

<Window>有一个属性 FontSize。

So you can set desired fontsize in element if you want to change the fontsize in all the elements within that window.

因此,如果您想更改该窗口内所有元素的字体大小,则可以在元素中设置所需的字体大小。

<Window FontSize="12">

</Window>

回答by Neal

Another option is to define the FontFamily and FontSize as resources.

另一种选择是将 FontFamily 和 FontSize 定义为资源。

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

That way you can use them in your setters.

这样你就可以在你的 setter 中使用它们。

回答by Vipul

Application.Current.MainWindow.FontSize = _appBodyFontSize;

This way you can change the Font Size at run time also.

这样您也可以在运行时更改字体大小。

回答by Lin

TextElement.FontSizeis an inherit property, which means you can simply set the font size at root element, and all the children elements will use that size (as long as you don't change them manually)

TextElement.FontSize是一个继承属性,这意味着您可以简单地在根元素设置字体大小,并且所有子元素都将使用该大小(只要您不手动更改它们)

回答by Chris Nicol

For any styles in WPF, you should have a separate resource dictionary that contains the styles for your app.

对于 WPF 中的任何样式,您都应该有一个单独的资源字典,其中包含您的应用程序的样式。

If you want to have a single Font Size that's reused throughout the app then just create a style for that font size. You can either give it a unique name/key to use explicitly or you can set a targetType that will transcend throughout the app.

如果您想要在整个应用程序中重复使用单个字体大小,那么只需为该字体大小创建一个样式。您可以给它一个唯一的名称/键以明确使用,或者您可以设置一个将超越整个应用程序的 targetType。

Explicit Key:

显式键:

<Style
    x:Key="MyFontSize"
    TargetType="TextBlock">
    <Setter
        Property="FontSize"
        Value="10" />
</Style>

<Control
    Style="{StaticResource MyFontSize}" />

*Note this style can be used with controls that have contentPresenters

*请注意,此样式可与具有 contentPresenters 的控件一起使用

For all textblocks in the app:

对于应用程序中的所有文本块:

<Style
    TargetType="TextBlock">
    <Setter
        Property="FontSize"
        Value="10" />
</Style>

<TextBlock
        Text="This text will be size 10" />

回答by Pablonete

If you need to programmatically change global FontSize, not statically (XAML), to be applied once for all your windows, you can do:

如果您需要以编程方式更改全局 FontSize,而不是静态 (XAML),以便为所有窗口应用一次,您可以执行以下操作:

TextElement.FontSizeProperty.OverrideMetadata(
            typeof(TextElement),
            new FrameworkPropertyMetadata(16.0));

        TextBlock.FontSizeProperty.OverrideMetadata(
            typeof(TextBlock),
            new FrameworkPropertyMetadata(16.0));

This values are applied to any TextBlock, Labels and almost any text in any windows, whereas it has not a explicit FontSize defined. But this does not affect for TextBox, you have to write a similar code for it or any other special controls.

该值适用于任何文本块、标签和几乎任何窗口中的任何文本,而它没有明确定义 FontSize。但这对 TextBox 没有影响,您必须为它或任何其他特殊控件编写类似的代码。

回答by Jim Balter

To dynamically change the font size globally with ctrl-mousewheel:

使用 ctrl-mousewheel 动态更改全局字体大小:

XAML:

XAML:

<Window Name="MainWindow" ... PreviewMouseWheel="MainWindow_PreviewMouseWheel">

code behind:

后面的代码:

private void MainWindow_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
    {
        if (e.Delta > 0)
            ++mainCtrl.FontSize;
        if (e.Delta < 0 && mainCtrl.FontSize > 1)
             --mainCtrl.FontSize;
    }
}