自定义样式的例子
代码
App.xaml
<Style x:Key="TextBlockStyleSample" TargetType="TextBlock">
<Setter Property="FontFamily" Value="Old English Text MT" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontSize" Value="68" />
<Setter Property="Foreground" Value="Green" />
<Setter Property="Background" Value="Black" />
</Style>
TextBlockStyleSample.xaml
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock
x:Name="ColorTimer"
Width="auto"
Height="auto"
Margin="0,20,0,0"
Style="{StaticResource TextBlockStyleSample}"
Text="ceshi" />
</StackPanel>
</Grid>
效果
当不能完整显示所有字符串时,自动显示省略号
代码
App.xaml
<Style x:Key="EllipsisStyle" TargetType="TextBlock">
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
TextBlockWithEllipsis.xaml
<Grid>
<TextBlock
Width="200"
Height="50"
HorizontalAlignment="Center"
Style="{StaticResource EllipsisStyle}"
Text="上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农"
TextWrapping="Wrap" />
</Grid>
效果