How can I center a Silverlight DataGridTemplateColumn header?
- by Mike Pateras
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?