简体   繁体   中英

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>

I am trying to apply Center text allignment to the GridCell but it does not work and looks weird. 在此处输入图片说明

Set the TextAlignment property to 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>
...

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM