只垂直居中了,没有水平居中
时间: 2023-07-12 20:31:46 浏览: 62
抱歉,我的回答有误。可以将Grid中的Border和Rectangle包裹在一个StackPanel中,并将StackPanel的HorizontalAlignment属性设置为Center,如下所示:
```xml
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid VerticalAlignment="Center">
<StackPanel HorizontalAlignment="Center">
<Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}">
<TextBlock Text="{Binding}" />
</Border>
<Rectangle Height="1" Fill="{Binding Foreground, ElementName=itemCtrl}" Margin="0,0,0,5">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
```
阅读全文