开发工具与关键技术:MVC、WPF
作者:刘俊杰
撰写时间:2019年05月16日
字体的属性不多,常用的就只有FontSize(字体大小)、FontWeight(字体加粗)、FontFamily(字体)、Foreground(字体颜色)这几种属性。字体属性用起来也很简单,只需要将字体的属性加入相应的控件中去就行了。
下面我们通过代码来了解一下字体的属性:
<Window x:Class="WpfApplication1.字体属性"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="字体属性" Height="300" Width="300">
<Grid>
<TextBlock Foreground="Red">字体颜色</TextBlock>
<TextBlock FontWeight="Bold" Margin="0,20,0,0">字体加粗</TextBlock>
<TextBlock FontSize="18" Margin="0,40,0,0">字体大小</TextBlock>
<TextBlock FontFamily="宋体" Margin="0,70,0,0">字体</TextBlock>
</Grid>
</Window>
显示的结果:
字体属性除了直接加在对应的控件中,还可以定义Style样式,在Style样式中改变文字的样式。
下面是引用样式代码:
<Window x:Class="WpfApplication1.字体属性"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="字体属性" Height="300" Width="300">
<!--资源-->
<Window.Resources>
<!--样式-->
<Style x:Key="Font" TargetType="TextBlock">
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Foreground" Value="Red"></Setter>
</Style>
</Window.Resources>
<Grid>
<TextBlock>字体样式</TextBlock>
<TextBlock Style="{StaticResource Font}" Margin="0,20">字体样式</TextBlock>
</Grid>
</Window>
显示结果: