Text alignment in a WPF DataGrid

后端 未结 13 783
野性不改
野性不改 2020-12-14 14:13

How can I align the column data to center in a WPF DataGrid?

13条回答
  •  忘掉有多难
    2020-12-14 14:39

    I really like Bruno's TextBlock.TextAlignment approach. You can use this in conjunction with horizontal alignment and then any background will stretch across the whole grid cell.

    e.g. (in VB)

        Dim sty = New System.Windows.Style(GetType(DataGridCell))
        sty.Setters.Add(New Setter(HorizontalAlignmentProperty, HorizontalAlignment.Stretch))
        sty.Setters.Add(New Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right))
        sty.Setters.Add(New Setter(BackgroundProperty, Brushes.LightGray))
    

提交回复
热议问题