views:

369

answers:

1

I want to center the header on a Silverlight DataGridTemplateColumn. The following code gets me most of the way there:

DataGridTemplateColumn column = new DataGridTemplateColumn();
column.CellTemplate = Resources[templateName] as DataTemplate;
column.Header = headerName;
column.HeaderStyle = new Style { TargetType = typeof(DataGridColumnHeader) };
column.HeaderStyle.Setters.Add(new Setter(DataGridColumnHeader.HorizontalAlignmentProperty, HorizontalAlignment.Center));

The header is, indeed, centered, but if the column is expanded, the header doesn't stretch. It just remains it's original width, leaving white gaps on either side of it, which looks terrible.

What is the proper way to center the column header, such that it still occupies the full width?

+1  A: 

Set the HorizontalContentAlignment Property to Center.

It seems that Content here refers to the content of the header, not the content of the cells in the datagrid.

Ryan Bates