WPF global font size

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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?

What is the best practice?

回答1:

I'd do it this way:

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)



回答2:

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

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

Add a style for Window to your ResourceDictionary

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

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


回答3:

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

Calibri12

That way you can use them in your setters.



回答4:

has a property FontSize.

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



回答5:

Application.Current.MainWindow.FontSize = _appBodyFontSize;

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



回答6:

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

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.

Explicit Key:

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

For all textblocks in the app:



回答7:

TextElement.FontSize is 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)



回答8:

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

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.



转载请标明出处:WPF global font size
文章来源: WPF global font size
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!