WPF设置字体和字号
最简单在MainWindow.xaml设置,字体 FontFamily,字号 FontSize
<Window x:Class="TestWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestWpf" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" FontFamily="Microsoft YaHei" FontSize="15">
后台设置
this.FontFamily = new FontFamily("Microsoft YaHei"); this.FontSize = 15;
动态设置,在页面选择字体,后台生效:
xaml中
<ComboBox Name="cmbFontFamily"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Source}" FontFamily="{Binding Source}" MouseDown="SelectFontFamily" /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
cs中
public MainWindow() { InitializeComponent(); //绑定字体数据源 ICollection<FontFamily> fontList = Fonts.SystemFontFamilies; this.cmbFontFamily.ItemsSource = fontList.OrderBy(o => o.Source); } //选择字体 private void SelectFontFamily(object sender, MouseButtonEventArgs e) { string wordStyle = ((TextBlock)sender).Text; this.FontFamily = new FontFamily(wordStyle); }
ComboBox绑定数据:https://www.zhihu.com/tardis/bd/art/426593951?source_id=1001
wpf内置字体:https://qa.1r1g.com/sf/ask/489561551/
后台修改字体(还有Application.Current.MainWindow.FontSize = 12;
):https://www.coder.work/article/6217691
app.xaml中设置,但不能动态修改:https://www.cnblogs.com/xizhun/articles/16858624.html