以下是一个样式,名为CenteredTextBlock:

<Style x:Key="CenteredTextBlock" TargetType="TextBlock">
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>

可以在需要使用此样式的TextBlock上应用它:

<TextBlock Style="{StaticResource CenteredTextBlock}" Text="Hello, world!"/>

如果需要在此基础上增加垂直和水平内容居中,可以定义一个新的样式,继承自CenteredTextBlock,然后在其中增加VerticalContentAlignment和HorizontalContentAlignment属性的设置:

<Style x:Key="CenteredAndAlignedTextBlock" TargetType="TextBlock" BasedOn="{StaticResource CenteredTextBlock}">
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>

同样可以在需要使用此样式的TextBlock上应用它:

<TextBlock Style="{StaticResource CenteredAndAlignedTextBlock}" Text="Hello, world!"/>
``
WPF 写一个样式有水平居中垂直居中字体16 名字叫textBlock在写一个文本样式继承textBlock增加VerticalContentAlignment、HorizontalContentAlignment 居中

原文地址: http://www.cveoy.top/t/topic/cv5m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录