2

I generate a Datagrid and want to center the content.

this is my code:

DataGrid tabelle = new DataGrid();
tabelle.ItemsSource = dt.DefaultView;
tabelle.RowHeight = 50;
tabelle.VerticalContentAlignment = VerticalAlignment.Center;
tabelle.HorizontalContentAlignment = HorizontalAlignment.Center;

It doesnt work... but why?

4
  • What do you mean 'doesn't work'? What do you see instead? Commented Jun 8, 2016 at 16:46
  • the content is not centered. I get the same result if I delete the last two lines Commented Jun 8, 2016 at 16:48
  • Is there a reason you are doing it through code? in XAML, you have more easy to apply options ex. Style. Also, I assume u are using MVVM or MVC since you have ViewModel defined Commented Jun 8, 2016 at 17:04
  • im new to wpf. i dont know how to generate something dynamically via xaml, so i did it via code. And i think it should work, but not the way i tried. Commented Jun 8, 2016 at 17:12

3 Answers 3

3

DataGridCell template does not use VerticalContentAlignment and HorizontalContentAlignment properties of DataGrid.

There are ways to get desired alignment. Take a look here: DataGrid row content vertical alignment

here is a solution in code

DataGrid tabelle = new DataGrid();
tabelle.ItemsSource = dt.DefaultView;
tabelle.RowHeight = 50;

// cell style with centered text
var cellStyle = new Style
                {
                    TargetType = typeof (TextBlock),
                    Setters =
                    {
                        new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center),
                        new Setter(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center),
                    }
                };

// assign new style for text columns when they are created
tabelle.AutoGeneratingColumn += (sender, e) =>
{
    var c = e.Column as DataGridTextColumn;
    if (c != null)
        c.ElementStyle = cellStyle;
};
Sign up to request clarification or add additional context in comments.

Comments

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

tabelle.CellStyle = cellStyle;

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

For autogenerated Columns, there is the AutoGeneratingColumn event that fires for every Column that gets autogenerated, and AutoGeneratedColumns that fires after all Columns are generated.

Private Sub dataGrid_AutoGeneratedColumns(sender As Object, e As EventArgs) Handles dataGrid.AutoGeneratedColumns
          ' This event gets fired after all Columns have been autogenerated.
          Dim dataGrid As DataGrid = CType(sender, DataGrid)
          Dim cellStyle As Style
    
          For intI As Integer = 0 To dataGrid.Columns.Count - 1
             cellStyle = New Style With {
                        .TargetType = GetType(DataGridCell)
                    }
             cellStyle.Setters.Add(New Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center))
             dataGrid.Columns(intI).CellStyle = cellStyle
          Next
    End Sub 

The other answers to this question needed slightly different Types to work for me or didnt include the event handling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.