1

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

enter image description here

Do you see that there is double bottom line?

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

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121

1 Answers1

2

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>
mm8
  • 163,881
  • 10
  • 57
  • 88