WPF - changing global font size at runtime

I need to add one functionality to my simple application - so that the user can change the font size for the entire application. Is it easy to do? Could you give me a hint how to start? It should have only 3 predefined font sizes, but the first and only solution that occurred to me was to create 3 different themes. Is it possible to simplify it?

+6
wpf xaml wpf-controls
source share
3 answers

Fortunately, FontSize uses the Inheritance property value . This means that until this is canceled, FontSize will automatically propagate to all child text elements. As a result, you can install one:

 <Window FontSize="10" ...> 

and it will apply to all text elements in this window that do not have font size. Changing it is also easy:

 this.FontSize = 20; 

in the code window of the window, all unspecified font sizes will be changed "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 that you mentioned.

+12
source share
 Application.Current.MainWindow.FontSize = 12; 
+3
source share

At the most basic level, you need to bind the FontSize property of your TextBlocks , etc. to a variable, which you can then change one of three predefined values:

 <TextBlock FontFamily="Arial" Text="Sample text" FontSize="{Binding TextSize}" /> 

However, you will not need to add this text to the text all .

The best solution would be to relate the size of the styles you use, but again, all the text should be styled. If you used an implicit style, you would not need to add a link to the text, but the whole text should look the same. Regardless of whether this problem will or will not depend on your application.

0
source share

All Articles