WPF - 在运行时更改全局字体大小 [英] WPF - change global font size at runtime

查看:59
本文介绍了WPF - 在运行时更改全局字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向我的简单应用程序添加一项功能 - 允许用户更改整个应用程序的字体大小.容易做到吗?你能给我任何提示如何开始吗?只需要 3 种预定义的字体大小,但我想到的第一个也是唯一的解决方案是创建 3 个不同的主题.有没有可能让它更简单?

I need to add one functionality to my simple application - to allow user to change font size for whole aplication. Is it easy to do? Can you give me any hint how to start? It's required to have only 3 predefined font sizes but the first and only solution which came to my mind is to create 3 different themes. Is it possible to make it simplier?

推荐答案

幸运的是,FontSize 使用了 属性值继承.这意味着只要不覆盖它,FontSize 将自动传播到所有子文本元素.因此,您可以设置单个:

Luckily, FontSize uses Property Value Inheritance. That means that so long as don't override it, FontSize will be automatically propagated to all child text elements. As a result, you can set a single:

<Window FontSize="10" ...>

它将应用于该窗口中没有字体大小的所有文本元素.在代码中更改它也很简单:

and it will apply to all text elements in that window that don't have a font size. To change it in code is simple as well:

this.FontSize = 20;

在窗口的代码隐藏中将动态更改所有未指定的字体大小.这也适用于似乎不支持字体大小的东西:

in the code-behind of the window will change all unspecified font sizes on the fly. This also works for things that don't seem to support font size:

<Grid TextElement.FontSize="15" ...>

您提到的其他文本属性也是如此.

The same is true for the other text properties you mentioned.

这篇关于WPF - 在运行时更改全局字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆