Text alignment in a WPF DataGrid

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

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

13条回答
  •  独厮守ぢ
    2020-12-14 14:44

    Here's @MohammedAFadil's XAML answer, converted to C# code behind:

    var MyStyle = new Style(typeof(DataGridCell)) {
        Setters = {
            new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
        }
    };
    

    To apply the Style, set the CellStyle property of the DataGrid, e.g.

    var MyGrid = new DataGrid() {
        CellStyle = MyStyle
    };
    

提交回复
热议问题