简体   繁体   中英

How to set DataGridTextColumn vertically center (WPF)?

I would like to numerate my rows in DataGrid , for this I am using such approach

https://stackoverflow.com/a/15061668/5709159

works nice, but number in cell not adjust center horizontally nor vertically.

Then I found such solution

https://stackoverflow.com/a/2729428/5709159

But this just center a number horizontally, but not vertically.

For vertical center I found such approach

https://stackoverflow.com/a/720824/5709159

but as wrote in comments

The text was centered, but the cell width no longer matched up with its header

So, I get something like this

在此处输入图片说明

Do you see that there is double bottom line?

So, question is - how to center a number horizontally as well as vertically?

You could define an ElementStyle :

<DataGridTextColumn Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, 
                        Converter={local:RowToIndexConverter}}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

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