Center 的 DataGridCell 样式不起作用

DataGridCell style of Center is not working

<DataGrid.Columns>
    <DataGridTextColumn Header="Id" Binding="{Binding Id, Mode=TwoWay}" IsReadOnly="True" Width="100">
        <DataGridTextColumn.CellStyle>
            <Style>
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"></Setter>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>
    <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay}" IsReadOnly="True" Width="100">
        <DataGridTextColumn.CellStyle>
            <Style>
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"></Setter>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>
    <DataGridTextColumn Header="Group" Binding="{Binding Group, Mode=TwoWay}" IsReadOnly="True" Width="100">
        <DataGridTextColumn.CellStyle>
            <Style>
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"></Setter>
            </Style>
        </DataGridTextColumn.CellStyle>
    </DataGridTextColumn>
</DataGrid.Columns>

我正在尝试对 GridCell 应用居中文本对齐方式,但它不起作用而且看起来很奇怪。

试试这个:

 <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="TextAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

TextAlignment 属性 设置为 Center:

<DataGridTextColumn Header="Id" Binding="{Binding Id, Mode=TwoWay}" IsReadOnly="True" Width="100">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>
...