DataGrid row content vertical alignment

后端 未结 9 927
借酒劲吻你
借酒劲吻你 2020-12-01 00:13

I have a regular DataGrid from WPF 4.0 RTM, where I put data from a database. In order to make clean & light style of DataGrid I use a tall/high rows and by

9条回答
  •  误落风尘
    2020-12-01 00:27

    Building on Jamier's answer, the following code did the trick for me when using auto-generated columns:

    Style VerticalCenterStyle = new Style();
    
    public MainWindow()
    {
      // This call is required by the designer.
      InitializeComponent();
    
      VerticalCenterStyle.Setters.Add(new Setter(VerticalAlignmentProperty, VerticalAlignment.Center));
    }
    
    private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {   
      if (e.Column is DataGridTextColumn) {
        ((DataGridTextColumn)e.Column).ElementStyle = VerticalCenterStyle;
      }
    }
    

提交回复
热议问题