自定义样式的例子

代码

App.xaml

  1. <Style x:Key="TextBlockStyleSample" TargetType="TextBlock">
  2. <Setter Property="FontFamily" Value="Old English Text MT" />
  3. <Setter Property="HorizontalAlignment" Value="Center" />
  4. <Setter Property="FontSize" Value="68" />
  5. <Setter Property="Foreground" Value="Green" />
  6. <Setter Property="Background" Value="Black" />
  7. </Style>

TextBlockStyleSample.xaml

  1. <Grid>
  2. <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
  3. <TextBlock
  4. x:Name="ColorTimer"
  5. Width="auto"
  6. Height="auto"
  7. Margin="0,20,0,0"
  8. Style="{StaticResource TextBlockStyleSample}"
  9. Text="ceshi" />
  10. </StackPanel>
  11. </Grid>

效果

image.png

当不能完整显示所有字符串时,自动显示省略号

代码

App.xaml

  1. <Style x:Key="EllipsisStyle" TargetType="TextBlock">
  2. <Setter Property="TextTrimming" Value="CharacterEllipsis" />
  3. <Setter Property="HorizontalAlignment" Value="Left" />
  4. <Setter Property="TextWrapping" Value="Wrap" />
  5. </Style>

TextBlockWithEllipsis.xaml

  1. <Grid>
  2. <TextBlock
  3. Width="200"
  4. Height="50"
  5. HorizontalAlignment="Center"
  6. Style="{StaticResource EllipsisStyle}"
  7. Text="上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农上官码农"
  8. TextWrapping="Wrap" />
  9. </Grid>

效果

image.png