Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - change global font size at runtime

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?

like image 632
tomo Avatar asked Dec 28 '10 21:12

tomo


2 Answers

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.

like image 172
Rick Sladkey Avatar answered Oct 14 '22 02:10

Rick Sladkey


Application.Current.MainWindow.FontSize = 12;
like image 25
Vipul Avatar answered Oct 14 '22 01:10

Vipul