How can I center a Silverlight DataGridTemplateColumn header?

Posted by Mike Pateras on Stack Overflow See other posts from Stack Overflow or by Mike Pateras
Published on 2010-05-19T20:32:25Z Indexed on 2010/05/21 18:00 UTC
Read the original article Hit count: 608

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?

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about header